Python_2_Codecademy_2_Strings &a

2016-04-15  本文已影响34人  张一闻

<a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>


课程页面:https://www.codecademy.com/

Strings

name = "ZHANG Yong"
name = '张雍'

Escaping characters

a = 'Python\'s a programming language'

Access by index

z = "zhang"[0]
g = "zhang"[4]

String methods

print len("ZHANG") # Console: 5
print len("ZHANG Yong") # Console: 10 空格也算
foo = "ZHANG Yong"
print len(foo) # Console: 10
print "ZHANG".lower() # Console: zhang
foo = "ZHANG"
print foo.lower() # Console: zhang
print str(7) # Console: 7 把数字7变成了string "7"
foo = 617
print str(foo) # Console: 617

Dot notation

String Concatenation

a = "ZHANG" + " " + "Yong"
print a # Console: ZHANG Yong
#
print "Python第一个公开发行版是在" + str(1991) + "年。" 
# Console: Python第一个公开发行版是在1991年。

String Formatting with %

# 替换一个:
print "%s第一个公开发行版是在1991年。" % ("Python")
# Console: Python第一个公开发行版是在1991年。
#
# 替换多个:
print "%s第%s个%s发行版是在1991年。" % ("Python", "一", "公开")
# Console: Python第一个公开发行版是在1991年。
name = raw_input("你叫什么名字? ")
theme = raw_input("你在写关于什么的blog? ")
place = raw_input("你在哪里? ")
print "现在%s正在%s写关于%s的博客。" % (name, place, theme)
""" 
如果我上述的input分别是:
你叫什么名字? 张雍
你在写关于什么的blog? Python
你在哪里? 家里
output则是:
现在张雍正在家里写关于Python的博客。
"""

String and for loop

for char in "I'm a string":
    print char,
Console:
I ' m   a   s t r i n g

Modifying strings

【自创练习1:把string中的a/A全部换成*】

def no_a(my_string):
    for i in range(len(my_string)):
        if my_string[i] == "a" or my_string[i] == "A":
            my_string[i] = "*" # error!
    return my_string

print no_a("Happy birthday!")
Output:
Traceback (most recent call last):
  File "/Users/y(***)zhang/PycharmProjects/untitled1/test.py", line 11, in <module>
    print no_a("Happy birthday!")
  File "/Users/y(***)zhang/PycharmProjects/untitled1/test.py", line 8, in no_a
    my_string[i] = "*"
TypeError: 'str' object does not support item assignment

Process finished with exit code 1

搜了一下发现:strings are immutable... 心碎

看来只能曲线救国了:

【方法一:】strings变成list,至少strings之间能concatenate,不算太差

def no_a(my_string):
    result = []
    for i in range(len(my_string)):
        if my_string[i] != "a" and my_string[i] != "A":
            result.append(my_string[i])
        else:
            result.append("*")
    for i in range(1, len(result)): 
        result[0] = result[0] + result[i]
    return result[0]
"""
上面可以多放个变量,
比如new_string = "", 
然后for loop里面不从1开始从0开始,
最后return new_string.
不过开始写程序的时候我懒了。(结果现在还要多打说明comment...)
"""

print no_a("Happy birthday!")
Console:
H*ppy birthd*y!

Process finished with exit code 0

【方法二:】用自带的.join(list)的功能,更加简单

def no_a(my_string):
    result = []
    for i in range(len(my_string)):
        if my_string[i] != "a" and my_string[i] != "A":
            result.append(my_string[i])
        else:
            result.append("*")
    """
    a = "".join(result)
    print type(a)
    结果是,join之后的格式是string: <type 'str'>
    """
    return "".join(result)
print no_a("Happy birthday!")
Console:
H*ppy birthd*y!

Process finished with exit code 0
上一篇 下一篇

猜你喜欢

热点阅读