Python学习笔记019

字符串操作

a="let's go"
print(a)

# 重复输出
print(a*2)

# 通过索引获取字符串中的字符,相当于切片
print(a[2:])

# 判断运算,关键字 in 字符串,返回 True / False
print('et' in a)

# 格式化字符串
print('%s is a teacher'%'kevin')

#字符串拼接,+ 效率低,.join效率高,可定制
a='123'
b='abc'
c=a+b
print(c)

d='---'.join([a,b,c])
print(d)

内置方法

 

# 内置方法
st='hello,kitty'

# 统计元素个数
print(st.count('l'))

# 首字母大写
print(st.capitalize())

# 第一个参数自定义长度,字符串居中,其他用第二个参数填充
print(st.center(50,'-'))

# 解码
print(st.encode())

# 判定字符串结尾,返回 True / False
print(st.endswith('y'))
# 判定字符串开头,返回 True / False
print(st.startswith('h'))

# 自定义空格数, tab键(\t)默认4个空格
st='hello,\tkitty'
print(st.expandtabs(tabsize=10))

# 从左向右查找到第一个元素,并返回索引值,-1代表没有找到
st='hello,kitty'
print(st.find('a'))

# 从右向左查找到第一个元素,并返回索引值,-1代表没有找到
print(st.rfind('t'))

# 格式化输出
st='hello,kitty {name} is {age}'
# 格式化输出,以赋值方式
print(st.format(name='xy',age=38))
# 格式化输出,以字典方式
print(st.format_map({'name':'kevin','age':35}))

# 查找到第一个元素,并返回索引值,没有找到 报错
st='hello,kitty'
print(st.index('l'))

# 判定字符串是否只包含数字或者字母或者汉字,返回 True / False
st1='hello,kitty is 8'
st2='hellokittyis8'
st3='中国'
print(st1.isalnum(),st2.isalnum(),st3.isalnum())

# 判定字符串是否为字母,返回 True / False
st='abcedfg'
print(st.isalpha())

# 判定字符串中数字是否为10进制,返回 True / False
st='159.999'
# isdecimal()
# True: Unicode数字,,全角数字(双字节)
# False: 罗马数字,汉字数字
# Error: byte数字(单字节)
print(st.isdecimal())
# isnumeric()
# True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
# False:
# Error: byte数字(单字节)
print(st.isnumeric())

# 判定字符串中数字是否为整数,返回 True / False
# isdigit()
# # True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
# # False: 汉字数字
# # Error:
st='159.999'
print(st.isdigit())

# 判定字符串中是否含有非法字符,返回 True / False
st='hellokitty'
print(st.isidentifier())

# 判定字符串是否全部小写,返回 True / False
st='hellokitty'
print(st.islower())

# 判定字符串是否全部大写,返回 True / False
st='HELLOKITTY'
print(st.isupper())

# 判定字符串是否全部空格,返回 True / False
st=' '
print(st.isspace())

# 判定字符串是否所有首字母都大写,返回 True / False
st='Hello,Kitty'
print(st.istitle())

# 把字符串中所有字母变成小写
st='Hello,Kitty'
print(st.lower())

# 把字符串中所有字母变成大写
st='Hello,Kitty'
print(st.upper())

# 把字符串中所有字母反转,小写变大写,大写变小写
st='Hello,Kitty'
print(st.swapcase())

# 第一个参数自定义长度,字符串居左,其他用第二个参数填充
st='Hello,Kitty'
print(st.ljust(50,'-'))
# 第一个参数自定义长度,字符串居右,其他用第二个参数填充
print(st.rjust(50,'-'))

# 把字符串中所有首末空格、换行符去掉
st=' \tHello, Kitty \n'
print(st)
print(st)

print(st.strip())
print(st.strip())

# 把字符串中左边空格、换行符去掉
print(st.lstrip())
print(st.lstrip())

# 把字符串中右边空格、换行符去掉
print(st.rstrip())
print(st.rstrip())

# 查找字符串中第一个参数的所有内容,然后用第二个参数来代替
st='Hello,Kitty,Litty'
print(st.replace('tty','tten'))
# 第三个参数表示替换几次
print(st.replace('tty','tten',1))

# 把字符串分割成列表,第一个参数为分隔符
st='Hello,Lea,Gerg,Fed'
print(st.split('e'))
# 第二个参数为从左向右,分隔的次数
print(st.split('e',2))
# name1='kitty','litty'
# name2='candy','mindy'
# print(','.join(name1))

# 把字符串分割成列表,第一个参数为分隔符
st='Hello,Lea,Gerg,Fed'
print(st.rsplit('e'))
# 第二个参数为从右向左,分隔的次数
print(st.rsplit('e',3))

# 把字符串每个单词首字母大写
st='Hello,lea,gerg,fed'
print(st.title())

#重要的字符串方法
st='Hello,lea,gerg,fed'
print(st.count('e'))
print(st.center(50,'-'))
print(st.startswith('H'))
print(st.find('o'))
st='hello,kitty {name} is {age}'
print(st.format(name='xy',age=38))
st='Hello,Lea,Gerg,Fed'
print(st.lower())
print(st.upper())
st=' \tHello, Kitty \n'
print(st)
print(st)
print(st.strip())
print(st.strip())
st='Hello,Lea,Gerg,Fed'
print(st.replace('Hello','Abb'))
print(st.split('e'))

 

 

posted @ 2020-03-05 21:48  wtzxxy  阅读(213)  评论(0)    收藏  举报