python3 字串基礎

2021-08-21 16:30:12 字數 664 閱讀 4888

字串可以使用一對單引號或一對雙引號指定起止位置,兩種方式指定的字串完全等價。

如'hello'和"world"

可以用三引號("""或''')指定多行字串,其中可自由使用單、雙引號而不需轉義。

如'''"what's your name?," i asked.'''

字串過長不方便寫在一行時,可以使用反斜槓跨行而不增加換行符。

如:"abcde\

fgh"

等價於"abcdefgh"

如果字串中有大量轉義字串,可以使用自然字串(在字串前加r或r)來簡化書寫,例如:r"newlines are indicated by \n"等價於"newlines are indicated by \\n"

兩個字串連續書寫會自動連線,例如:str='ab'"cd",str就是'abcd'

可以使用format來構造複雜字串,{}表示引數位置,format中依次指定引數值:(和%方式功能類似,但更加靈活。)

python 快速教程(補充篇05):字串格式化 (%操作符)

python - 增強的格式化字串format函式

pep 3101 -- advanced string formatting

字串格式化:%和.format

python中的字串模運算子與format函式

python3字串相等 python3 字串

1 拼接 1 多個字串進行連線 連線符,必須左右資料型別一致 例 print hello world 結果 helloworld 例 print 5 world 結果 typeerror unsupported operand type s for int and str 2 多個相同字串連線 字串...

python3 字串操作

auther aaron fan name my tname is age is print name.capitalize 這段話的首字母大寫 print name.count a 統計這段字串中一共有多少個a print name.casefold print name.center 50,一共...

python3 字串運算

運算子含義 字串連線 重複輸出字串 通過索引獲取字串中字元 擷取字串中的一部分 in成員運算子 如果字串中包含給定的字元返回 true not in 成員運算子 如果字串中不包含給定的字元返回 true r r原始字串 原始字串 所有的字串都是直接按照字面的意思來使用,沒有轉義特殊或不能列印的字元。...