python

超轻量级php框架startmvc

python字符串string的内置方法实例详解

更新时间:2020-06-02 03:00:02 作者:startmvc
下面给大家分享python字符串string的内置方法,具体内容详情如下所示:#__author:"PizerWang"#__dat

下面给大家分享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字符串内置方法 python字符串string