<a href="http://www.lxweimin.com/p/54870e9541fc">總目錄</a>
課程頁面:https://www.codecademy.com/
Strings
- 簡言之就是雙/單引號里面加上數字、字母、符號等。
name = "ZHANG Yong"
name = '張雍'
Escaping characters
- 電腦經常會誤會真·引號和作為標記strings的引號。為了告訴目前智商雖然在急速增長中但仍然常常不夠的電腦什么是真·引號,就要加上一個backslash.
a = 'Python\'s a programming language'
Access by index
- 每個string中每個character都有個index。
- index從0開始算。
z = "zhang"[0]
g = "zhang"[4]
String methods
-
len()
: 計算字符長度
print len("ZHANG") # Console: 5
print len("ZHANG Yong") # Console: 10 空格也算
foo = "ZHANG Yong"
print len(foo) # Console: 10
-
lower()
: 變成lowercase
print "ZHANG".lower() # Console: zhang
foo = "ZHANG"
print foo.lower() # Console: zhang
-
upper()
: 變成uppercase -
str()
:把非string的data type變成string
print str(7) # Console: 7 把數字7變成了string "7"
foo = 617
print str(foo) # Console: 617
Dot notation
- 為什么
str()
和len()
都是直接把對象放到括號里就好了,但是.upper()
和.lower()
這種卻要dot notation呢? - 因為dot notation只能用于data type為string的類型。而
str()
這種可以用于多重data types.
String Concatenation
- 一個加號
+
就能concatenate不同的strings了。
a = "ZHANG" + " " + "Yong"
print a # Console: ZHANG Yong
#
print "Python第一個公開發行版是在" + str(1991) + "年。"
# Console: Python第一個公開發行版是在1991年。
String Formatting with %
- 用
%
來把strings和variables關聯更加方便。
# 替換一個:
print "%s第一個公開發行版是在1991年。" % ("Python")
# Console: Python第一個公開發行版是在1991年。
#
# 替換多個:
print "%s第%s個%s發行版是在1991年。" % ("Python", "一", "公開")
# Console: Python第一個公開發行版是在1991年。
- 一個結合
raw_input()
的例子:
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