string
常用表示方法
python字符串常用表示方式:
使用单引号(‘)
用单引号定义字符串。所有的空白,即空格和制表符都照原样保留。
使用双引号(")
python中使用双引号与单引号的字符串完全相同。
使用三引号(’''或"“”)
利用三引号,你可以指示一个多行的字符串。你可以在三引号中自由的使用单引号和双引号。
(字符串中可以包含数字、字母,以及一些控制字符,如换行符、制表符等)
string方法
# edited by Lyu
# 仅供学习使用,禁止一切商业用途
name=" My \tname is lyu!\n"
print(name.capitalize())
print(name.upper())
print(name.lower())
print(name.strip())
print(name.lstrip())
print(name.rstrip())
print(name.count("y"))
print(name.center(50,"-"))
print(name.ljust(50))
print(name.rjust(50))
print(name.expandtabs(30))
print(name.find(" ")) #Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].
print('324'.endswith('4')) #Return True if S ends with the specified suffix, False otherwise.
print('324'.endswith('1'))
name2="My name is {name}"
print(name2.format(name="Lyu"))
print(name2.format_map({"name":"Lyu"}))
print('324'.isalnum())
print('324abc!'.isalnum())
print('abc'.isalpha())
print('324abc'.isalpha())
decimal="\u0033"
nondecimal="\u0055"
print(decimal.isdecimal())
print(nondecimal.isdecimal())
print(nondecimal.isidentifier()) #Return True if the string is a valid Python identifier, False otherwise.
print("324".isidentifier())
print("My Name".istitle())
print("my name".istitle())
print("+".join(['1','2','3'])) #Concatenate any number of strings.
运行结果如下:
my name is lyu!
MY NAME IS LYU!
my name is lyu!
My name is lyu!
My name is lyu!
My name is lyu!
2
---------------- My name is lyu!
----------------
My name is lyu!
My name is lyu!
My name is lyu!
0
True
False
My name is Lyu
My name is Lyu
True
False
True
False
True
False
True
False
True
False
1+2+3
进程已结束,退出代码为 0
读者可以自行尝试一下各种string方法,熟悉一下string的用法。