Zi 字媒體
2017-07-25T20:27:27+00:00
http://pythoncat.blogspot.tw/2014/11/python.html
字串處理
1. print() 字串列印
Ex:
print (“Hello, Daisy!”)
Hello, Daisy!
2. replace() 字串取代
Ex1:
>>> s = “$1,000.00”
>>> s1 = s.replace(“$”,””)
>>> print(s1)
1,000.00
Ex2(替代多個字元):
>>> s = “$1,000.00”
>>> s1 = s.replace(“$”,””)
>>> s = “$1,000.00”
>>> s1 = s.replace(“$”,””).replace(“,”,””)
>>> print(s1)
1000.00
3. strip() 字串移除空白
Ex1(移除頭尾側空白):
>>> s = ” $1,000.00 “
>>> s1 = s.strip()
>>> print(s1)
$1,000.00
Ex2(僅移除右側空白):
s = ” $1,000.00 “
>>> s1 = s.rstrip()
>>> print(s1)
$1,000.00
Ex3(僅移除左側空白):
s = ” $1,000.00 “
>>> s1 = s.lstrip()
>>> print(s1)
$1,000.00
4. end=”” 移除換行
python 3 於 print 後,會自動換行,像是自動帶了 C 語言的 n。若要移除換行符號請參考範例:
移除換行符號前:
for i in range(1,5):
print (i)
1
2
3
4
移除換行符號後:
for i in range(1,5):
print (i,end=”)
1234
美食
寫了
5860316篇文章,獲得
23313次喜歡