python 基礎3 使用字串

2021-07-23 16:41:33 字數 1252 閱讀 9475

>>> format  = "hello %s , how are %s "#%s的部分成為轉化說明符,標記了需要插入的位置,s表示值會被格式化為字串

>>> value = ('world','you')

>>> print format % value#使用元組

hello world , how are you

>>> format = "i am %.2f kg"#格式化為兩位數的浮點數

>>> value = (70.0)

>>> print format % value

i am 70.00 kg

find方法

在較長的字串中查詢出子串,返回子串所在位置最左端的索引,若沒有找到就返回-1

>>> name = "alshadanoweisiji"

>>> name.find("now")

7>>> name.find("frank")

-1 jon方法:

>>> li = ["1","2","3"]

>>> add = "+"

>>> add.join(li)

>>> '/'.join(["home","fffupeng","dasktop"])

'home/fffupeng/dasktop'#由此可見這是乙個非常重要的方法

lower方法:返回自字串的小寫字母版

>>> name = "fffupeng"

>>> name.lower()

'fffupeng'

replace:

>>> w = "fffupeng is handsome"

>>> w.replace("handsome","very handsome")

'fffupeng is very handsome'

split: 分割,join的逆方法

>>> "1+2+3+4".split("+")

['1', '2', '3', '4']

strip:去掉字串兩側的空白

>>> " fffupeng ".strip()

'fffupeng'

translate:替換單個字元

>>> from string import maketrans

>>> table = maketrans('fp','fp')

>>> "fffupeng".translate(table)

'fffupeng'

Python入門3 之使用字串

1,字串的格式化 format hello s 對應有 d,f value king print format value hello king print format king hello king v s is s print v king me 報錯,不是元組,後面的元素多於乙個就要寫成元組...

python基礎知識(2) 使用字串

在這裡會介紹使用字串格式化其他值,並簡單了解一下利用字串的分割 連線 搜尋等方法能做些什麼。基本字串操作 所有標準的序列操作 索引 分片 乘法 判斷成員資格 求長度 取最大值 取最小值 同樣適用。字串是不可變的,下面看乙個例項說明這點。website website 3 com traceback ...

《python基礎教程》第3章使用字串 讀書筆記

1.字串格式化操作符是乙個百分號 2.只有元組和字典可以格式化乙個以上的值。列表或者其他序列只會被解釋為乙個值。3.in操作符只能查詢字串中的單個字元。4.字串方法 find find方法可以在乙個較長的字串中查詢子串,它返回子串所在位置的最左端索引,如果沒有找到則返回 1。這個方法還能提供起始點和...