Python 字符串内置函数(一)

Python 字符串内置函数(一)
'''
可以通过dir(s)查看 其中s为字符串
'''
s = "heLLo WoRld!"
# 查看字符串s内置函数
print(dir(s))
'''
运行结果中的主要函数:
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index',
'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper',
'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
下面将按照使用频率一一介绍:
'''
# 1.字母大小写转换

s = "heLLo WoRld!"

# upper()函数是将字符串中所有英文字符都转化为大写字母
print(s.upper()) # 结果:HELLO WORLD!

# lower()函数是将字符串中所有英文字符都转化为大写字母
print(s.lower()) # 结果:hello world!

# capitalize()函数是将字符串首字母转化为大写字母,其余小写
print(s.capitalize()) # 结果:Hello world!

# swapcase()函数是将字符串中的字母大小写互换
print(s.swapcase()) # 结果:HEllO wOrLD!

# title()函数是将字符串每个单词首字母转换为大写字母,其余小写
print(s.title()) # 结果:Hello World!
 
























猜你喜欢

转载自www.cnblogs.com/shnuxiaoan/p/12934366.html
今日推荐