Python3 字串用法

2021-10-25 00:22:45 字數 795 閱讀 7433

在python中即可以使用雙引號""也可以使用單引號''對字串進行賦值

例如

name = "hello world"
也可以

name = 'he said "hello world"'
在python 常見的字串使用

name.title() :將單詞的首字母大寫

name.upper():將字串改為全部大寫

name.lower():將字串改為全部小寫

name ='hello  world'

name = name.title();

#hello world

name = name.upper();

#hello world

name = name.lowe()

#hello world

字串拼接 :直接用「+」號來合併字串,但是如果型別一致需要轉換

first_name = 'fly'

last_name = 'chen'

name = first_name +" "+last_name

age = 23

#這樣就會出錯 python 並不能解讀出這個數 因此將int 轉換為str

刪除字串末尾空白

message = 'i like it '

#'i like it '

message = message.restrip()

python3字串相等 python3 字串

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

python3 字串基礎

字串可以使用一對單引號或一對雙引號指定起止位置,兩種方式指定的字串完全等價。如 hello 和 world 可以用三引號 或 指定多行字串,其中可自由使用單 雙引號而不需轉義。如 what s your name?i asked.字串過長不方便寫在一行時,可以使用反斜槓跨行而不增加換行符。如 abc...

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,一共...