String函数
str.capitalize() : 字符串首字母大写
1 2 |
str = "hello world" print(str.capitalize()) |
输出 "Hello world"
str.replace() : 替换相应字符串
1 2 |
str = "hello world" print(str.replace("hello" , "good")) |
输出 "good world"
另外,str.replace() 还可以配置替换次数。
str.replace(old, new [, count]) 其中count就是设定替换的次数。
1 2 |
str = "123123123" print(str.replace("1" , "x", 2)) |
输出 "x23x23123"
str.split() : 字符串拆分
1 2 |
str = "192.168.1.1" print(str.split(".")) |
输出列表 ["192", "168", "1", "1"]
和str.replace()一样,str.split() 也可以配置分割次数。
1 2 |
str = "192.168.1.1" print(str.split(".",2)) |
分割2次,输出列表 ["192", "168", "1.1"]