python字符串三种常用的方法或函数_python中字符串常用的函数
s ="hello world"
s[2] >>> "l" 索引
s.index("l") >>> 2 索引值 (返回第一個(gè))
s[0:3] >>> "hel" 切片 (冒號(hào)左側(cè)索引值開(kāi)始,右側(cè)索引值前一位結(jié)束,-1 代表最后一位)
id(s) >>> 34733680 內(nèi)存地址編號(hào)
len(s) >>> 11 字符串的長(zhǎng)度
"h" in s >>> True 判斷字符是否在字符串中
max(s) >>> "w" 返回字符串中的最大值(字母返回索引最大的)
min(s) >>> " " 返回字符串中的最小值(字母返回索引最小的)
ord("!") >>> 33 返回字符utf-8 中的十進(jìn)制位置
chr(33) >>> "!" 返回當(dāng)前位置的字符
"I like %s %s" %("python","!") >>>I like python ! 格式化輸出 (%s 代表著占位符,用%后面()中內(nèi)容代替 %s代表字符,%d 代表整數(shù),%f 代表浮點(diǎn)數(shù))
dir(s) >>> .... 返回字符串常用的方法
s.isalpha() >>> False 判斷字符是否全是字母如果全是返回True
s.split(" ") >>> ['hello', 'world'] 通過(guò)什么字符將字符分割成列表
s.strip() >>>"hello world" 去掉字符的左右空格
s.upper() >>> "HELLO WORLD" 字符變大寫
s.isupper() >>>False 字符是否全大寫
s.lower() >>> "hello world" 字符變小寫
s.islower() >>> True 字符是否全小寫
s.title() >>> "Hello World" 首字母變大寫
s.istitle() >>> False
s.capitalize() >>> "Hello world" 字符首字母大寫 (capitalize)
增加:
s.replace(“舊字符”,"新的字符") #把舊的字符替換成新的字符
s.isdigit() # 判斷字符串是不是數(shù)字
總結(jié)
以上是生活随笔為你收集整理的python字符串三种常用的方法或函数_python中字符串常用的函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2019黑马python面试资料_201
- 下一篇: python大数据项目_(价值1280)