# 设置指定位置
s ="{0} {1}".format("hello","word")print(s)
s ="{0} {0}".format("hello","word")print(s)
s ="{1} {1}".format("hello","word")print(s)
s ="{1} {0}".format("hello","word")print(s)
hello word
hello hello
word word
word hello
# 使用命名参数
s ="我是{name},我最喜欢的女人是{lover}"print(s.format(name="Bidding",lover="Ayue"))# **表示解包操作
s_obj ={"name":"Bidding","lover":"Ayue"}print(s.format(**s_obj))
我是Bidding,我最喜欢的女人是Ayue
我是Bidding,我最喜欢的女人是Ayue
str内置函数
python中用str表示字符串
help(str)
字符串查找类,find,index
find:查找字符串是否包含子串,返回找到的子串位置,即第一次出现的位置,找不到返回-1
index:查找字符串是否包含子串,返回找到的子串位置,即第一次出现的位置,找不到抛出错误
lfind:从左往右查找
rfind:从右往左查找
字符串判断类
一般以is开头
isalpha:判断是否是字母
默认的前提是字符串必须包含至少一个字符,没有返回False
汉字被认为是alpha,因此此函数不能作为区分字母还是汉字,而是使用Unicode码
isdigit,isnumeric,isdecimal
判断数字使用正则表达式,不要使用上面这三个,有坑
islower:是否是小写
isupper:是否是大写
help(str.index)
Help on method_descriptor:
index(...)
S.index(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the substring is not found.