iT邦幫忙

2022 iThome 鐵人賽

DAY 4
0
  • 字串連結
  • 在字串中跳脫順序
  • 字串格式化
    • str.format
    • f-Strings
  • 字串與字元
    • 片取字串
    • 反轉字串
    • 跳取字串
  • 字串方法

這篇文章是閱讀Asabeneh的30 Days Of Python: Day 4 - Strings後的學習筆記與心得。


基本上字串的定義跟JavaScript(以下簡稱JS)差不多?任何被單、雙、三引號包覆的資料都會成為字串:

letter = 'A'
greeting = "Hi! There"

len(<string>)
Python對字串有方法(methods)及內建函式,比如len(letter)可以得到letter("A")的字串長度等於1。

跟JS用`(backtick)不同,跨多行字串要用三個單引號(''')或是三個雙引號(""")包覆:

ithelp_14th = '''I am learning Python.
I will do it for 30 days.
To seize foundamental of Python.'''

ithelp_14th_zh = """學習Python。
持續30天。
掌握Python基礎。"""

字串連結

就像JS,字串可以用+連結起來:

comment = "good"
food = "hamburger"
review = comment + ' ' + food
print(review) # 'good hamburger'

在字串中跳脫順序

這部份與JS差不多:

  • \n:接新的一行
  • \t:Tab,預設是推到第八格,少的部分會補空格,會在str.expandtabs(pos)做更多說明。
  • \:跳脫字元,像\'",如果要在字串中使用的話,必須在前面加上跳脫字元
# 看起來像一行字
print("\t\'2022iThome\'\n \t\\  鐵人賽  \\\n8/1開始報名\t9/1-9/16熱血開賽\n   \"再一步 世界就在你的腳下\"")

""" 實際輸出多行
        '2022iThome'
        \  鐵人賽  \
8/1開始報名     9/1-9/16熱血開賽
   "再一步 世界就在你的腳下"
"""

字串格式化

早期的作法是像C語言,使用%運算子,這個運算子是用來格式化"tuple"(大小固定的list)內的變數的,這裡面包括一般字串與引數區分符(argument specifiers):

  • %s - String (或是任何能代表字串的物件,如數字)
  • %d - 整數
  • %f - 浮點數
  • %.<number of digits>f - 固定精度浮點數
# 純字串
place = "Taiwan"
name = "HINNAMNOR"
time = "this week"
forecast = "%s, a typhoon - %s may go towards %s" %(time, name, place)

print(forecast) # this week, a typhoon - HINNAMNOR may go towards Taiwan

# 字串搭配數字
weight = int(input("What's your weight in kilogram: "))
height = int(input("What's your height in centermeter: "))
bmi = weight / (height / 100) ** 2
result = """
For your
weight: %d
height: %d
The body mass index is %.2f
""" %(weight, height, bmi)

print (result)
""" output:
For your
weight: 52
height: 155
The body mass index is 21.644121
"""

更多線上範例可以參考:
https://www.learnpython.org/en/String_Formatting

str.format

自Python 3引進了新的字串格式化作法:

# 純字串
place = "Taiwan"
name = "HINNAMNOR"
time = "this week"
forecast = "{}, a typhoon - {} may go towards {}".format(time, name, place)

print(forecast)

# 字串搭配數字
weight = int(input("What's your weight in kilogram: "))
height = int(input("What's your height in centermeter: "))
bmi = weight / (height / 100) ** 2
result = """
For your
weight: {}
height: {}
The body mass index is {:.2f}
""".format(weight, height, bmi)

print (result)

f-Strings

自Python 3.6引進了f-strings,又另一個字串格式化作法,類似JS中${<variable>}的方式:

# 純字串
place = "Taiwan"
name = "HINNAMNOR"
time = "this week"
forecast = (f"{time}, a typhoon - {name} may go towards {place}")

print(forecast)

# 字串搭配數字
weight = int(input("What's your weight in kilogram: "))
height = int(input("What's your height in centermeter: "))
bmi = weight / (height / 100) ** 2
result = (f"""
For your
weight: {weight}
height: {height}
The body mass index is {bmi:.2f}
""")

print (result)

字串與字元

這點跟JS差不多,而且Python也可以用像是解構賦值(Destructuring assignment)的方式拿到字串中的字元:

language = "Python"
a,b,c,d,e,f = language
print(a,b,c,d,e,f) # P y t h o n

或是像在JS中我們用string[i]拿字串中第i個位置的字元一樣,只是在Python中是list和tuple,而且可以接受負值作為索引值:

language = "Python"
print(language[1]) # y
# 拿倒數第一個字元
print(language[-1]) # n
print(language[-3]) # h

片取字串

要做到JS中slice(start[, endBefore])效果的話:

language = "Python"
print(language[0:3]) # Pyt
print(language[-3:]) # hon
print(language[3:]) # hon
  • 也跟JS的String.slice()一樣,從起點往右邊算取值,不會向左所以:
print(language[-3:0]) # ""

反轉字串

要做到JS中String.split("").reverse().join("")的效果的話:

language = "Python"
print(language[::-1]) # nohtyP

跳取字串

這個JS沒有,表現上是String.slice()但可以指定step來跳著取:

language = "Python"
print(language[0:6:2]) # Pto
  • 位置的意義是:[startPoint:endBefore:step]

字串方法

capitalize():把第一個字轉換成大寫

print("hello world!".capitalize()) # 'Hello world!'

title():把每個詞的第一個字轉換成大寫

print("thirty days of python".title()) # Thirty Days Of Python

swapcase():將字串中的大寫字元轉乘小寫,小寫轉成大寫

print("thirty days of python".swapcase()) # THIRTY DAYS OF PYTHON
print("thirty days of python".title().swapcase()) # tHIRTY dAYS oF pYTHON

count(substring[, start, endBefore]):計算該字串中substring的數目

challenge = "thirty days of python"
print(challenge.count("y")) # 3
print(challenge.count("y", 7, 14)) # 1 - "y" in "days"
print(challenge.count("th")) # 2

startwith():回傳字串的開頭是否為substring的布林值

print("thirty days of python".startswith("thirty")) # True
print("30 days of python".startswith("thirty")) # False

endwith(substring):回傳字串的結尾是否為substring的布林值

challenge = "thirty days of python"
print(challenge.endwith("on")) # True
print(challenge.endwith("tion")) # False

expandtabs(tabsize):修改tab字元代表的空格數,預設是8

challenge = "over\there"
print(challenge.expandtabs()) # 'over    here'
print(challenge.expandtabs(10)) # 'over      here'

\t是推到第八格
參考 -- Python expandtabs string operation
簡單來說,如果\t前面有其他字元它不是直接加8個空格,而是:|前面的字元數 - 8 (tabsize)| = 8 (tabsize) - 會補的空格數。
expandtabs(tabsize)改變的是基準:

challenge = "over\there"
# "over" (4 chars) - 8 (tabsize) 的絕對值等於4 = 8 (tabsize) - 4 (補4個空格)
print(challenge.expandtabs()) # 'over    here'
# "over" (4 chars) - 4 (tabsize) 的絕對值等於0 = 4 (tabsize) - 4 (補4個空格)
print(challenge.expandtabs(4)) # 'over    here'

find(substring[, start[, end]]):回傳第一個發現的substring的索引值,如果沒有,回傳-1

challenge = "happy holiday"
print(chanllenge.find("h")) # 0

rfind(substring[, start[, end]]):回傳最後一個發現的substring的索引值,如果沒有,回傳-1

challenge = "happy holiday"
print(chanllenge.rfind("h")) # 6

format():前面的字串格式化中有提及,更多應用可以參考這裡

index(substring[, start[, end]]):類似find()但如果沒找到,會丟ValueError

challenge = "happy holiday"
print(chanllenge.index("h", 3)) # 6
print(chanllenge.index("h", 3, 6)) # ValueError

rindex(substring[, start[, end]]):類似rfind()但如果沒找到,會丟ValueError

challenge = "happy holiday"
print(chanllenge.rfind("h", 0, 5)) # 0
print(chanllenge.rfind("h", 1, 5)) # ValueError

isalnum():回傳字串中字元是否全為字母數字(alphanumeric characters)的布林值

print("2022ironman".isalnum()) # True
print("TwentyTwentyTwoIronman".isalnum()) # True
print("2022 Ironman".isalnum()) # False - space is not an alphanumeric character

isalpha():回傳字串中字元是否全為字母(alphabet characters(a-zA-Z))的布林值

print("2022ironman".isalpha()) # False
print("TwentyTwentyTwoIronman".isalpha()) # True

isdecimal():回傳字串中字元是否全為數字(decimal(0-9))的布林值

print("123".isdecimal()) # True
print("\u00B2".isdecimal()) # False

isdigit():類似isdecimal()但接受數字的unicode字元

print("123".isdigit()) # True
print("\u00B2".isdigit()) # True
print("10.5".isdigit()) # False

\u00B2 = ²
https://www.fileformat.info/info/unicode/char/b2/index.htm

isnumberic():類似isdigit()但接受更廣義的數字unicode字元,像是½

print("10".isnumeric()) # True
print("\u00BD".isnumeric()) # True
print("10.5".isnumeric()) # False

\u00BD = ½
https://www.fileformat.info/info/unicode/char/00bd/index.htm

isidentifier():回傳一個字串是否可作為變數名稱的布林值

print("30DaysOfPython".isidentifier()) # False - cannot start with a number
print("thirty_days_of_python".isidentifier()) # True

islower():回傳是否一個字串全為小寫的布林值;數字和符號不影響結果

print("30days_of_python".islower()) # True
print("\u00b2".islower()) # False

isupper():回傳是否一個字串全為大寫的布林值;數字和符號不影響結果

print("30DAYS-OF-PYTHON".isupper()) # True
print("\U00B2".isupper()) # SyntaxError

join(iterable):類似JS的Array.join(),但分隔符號不是作為參數而是作為呼叫方法的物件

web_tech = ["HTML", "CSS", "JavaScript", "React"]
result = "#".join(web_tech)
print(result) # HTML#CSS#JavaScript#React

JS中是web_tech.join("#")

strip([chars]):類似JS的trim(),可以移除字串頭尾的空格,但不同的是,也接受給引數從字串頭尾中移除引數中包含的字元

spaces = "  hello  "
speaking = "impossible"
print(spaces.strip()) # 'hello'
print(speaking.strip("im")) # possible
print(speaking.strip("os")) # impossible - not begin/end with "os"

replace(old, new[, count]):將字串中old的值換成new的值,如果有count,則只替換前count個符合值

"assessibility".replace("s", "c", 2)

split(sep=None, maxsplit=-1):類似JS的String.split用給定的sep分割字串中的字元,不同的地方在於,JS中給第二參數會讓回傳值只提取到maxsplit個值,但Python中則是會將剩餘的值留下來放在最後

print("1, 2, 3".split(",")) # ['1', '2', '3']
print("1, 2, 3".split(",", maxsplit=1)) # ['1', '2, 3']

In JS: "1, 2, 3".split(",", maxsplit=1) returns ['1']


上一篇
【Day 3】運算子
下一篇
【Day 5】串列
系列文
從前端角度看30天學Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言