python字符串string的内置方法实例详解
2019/7/15 0:47:08
本文主要是介绍python字符串string的内置方法实例详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
下面给大家分享python 字符串string的内置方法,具体内容详情如下所示:
#__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print(a) print("-------------------") a = 'Let\'s go' print(a) print("-------------------") print("hello" * 3) print("helloworld"[2:]) print("-------------------") print("ell" in "helloworld") print("-------------------") print("Pizer is a good student") print("%s is a goog student" % "Pizer") print("-------------------") a = "1234" b = "abcd" c = "!@#$" d = a + b + c print(d) d = "".join([a, b, c]) print(d) d = ", ".join([a, b, c]) print(d) d = "++".join([a, b, c]) print(d) print("-------------------") print("string的内置方法") str = "helloworld" print(str.count("l")) #统计元个数 print(str.capitalize()) #首字母大写 print(str.center(25, "-")) #居中 print(str.endswith("d")) print(str.endswith("world")) print(str.endswith("word")) #是否以某个内容结尾 print(str.startswith("hello")) #是否以某个内容开始 str = "hello\tworld" print(str.expandtabs(tabsize=10)) print("-------------------") str = "helloworld {name} is {age}" print(str.find("w")) #查找到第一个元素并将索引值返回 print(str.format(name = "Pizer", age = 18)) print(str.format_map({"name":"Jone", "age":25})) print("-------------------") print(str.index("w")) #print(str.index("www")) #报错 print(str.find("wwww")) print("-------------------") str = "123abc" print(str.isalnum()) str = "123" print(str.isalnum()) str = "abc" print(str.isalnum()) str = "!@$" print(str.isalnum()) str = "中国万岁" print(str.isalnum()) print("-------------------") print("123456".isdecimal()) print("123456ff".isdecimal()) print("123456789".isdigit()) print("12345.6789".isdigit()) print("12345.6789".isnumeric()) print("-------------------") print("34abc".isidentifier()) print("_34abc".isidentifier()) print("abc".islower()) print("abC".islower()) print("ABC".isupper()) print(" ".isspace()) print("-------------------") print("Hello Jone".istitle()) print("Good morning".istitle()) print("-------------------") print("Hello Jone".lower()) print("Good morning".upper()) print("Hello Jone".swapcase()) print("-------------------") print("Hello world".ljust(20, "-")) print("Hello world".rjust(20, "-")) print(" Hello world \t \n") print(" Hello world ".strip()) print(" Hello world ".lstrip()) print(" Hello world ".rstrip()) print("-------------------") print("Hello Jone Jone".replace("Jone", "Pizer")) print("Hello Jone Jone".replace("Jone", "Pizer", 1)) print("My title".find("t")) print("My title".rfind("t")) print("-------------------") print("Hello world".split(" ")) print("Hello world".split("l", 1)) print("Hello world".rsplit("l", 1)) print("hello jone".title()) print("-------------------") #重要的字符串方法 # print(st.count('l')) # print(st.center(50,'#')) # 居中 # print(st.startswith('he')) # 判断是否以某个内容开头 # print(st.find('t')) # print(st.format(name='alex',age=37)) # 格式化输出的另一种方式 待定:?:{} # print('My tLtle'.lower()) # print('My tLtle'.upper()) # print('\tMy tLtle\n'.strip()) # print('My title title'.replace('itle','lesson',1)) # print('My title title'.split('i',1))
执行结果:
Let's go
Let's go
hellohellohello
lloworld
True
Pizer is a good student
Pizer is a goog student
1234abcd!@#1234abcd!@#
1234abcd!@#
1234, abcd, !@#$
1234++abcd++!@#$
string的内置方法
3
Helloworld
——–helloworld——-
True
True
False
True
hello world
5
helloworld Pizer is 18
helloworld Jone is 25
5
-1
True
True
True
False
True
True
False
True
False
False
False
True
True
False
True
True
True
False
hello jone
GOOD MORNING
hELLO jONE
Hello world———
———Hello world
Hello world
Hello world
Hello world
Hello world
Hello Pizer Pizer
Hello Pizer Jone
3
5
[‘Hello', ‘world']
[‘He', ‘lo world']
[‘Hello wor', ‘d']
Hello Jone
总结
以上所述是小编给大家介绍的python字符串string的内置方法实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!
这篇关于python字符串string的内置方法实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Python基础编程
- 2024-11-25Python编程基础:变量与类型
- 2024-11-25Python编程基础与实践
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南