码迷,mamicode.com
首页 > 编程语言 > 详细

Python 字符串内置函数(一)

时间:2020-05-22 00:04:34      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:字符   make   频率   大小   字符串   sla   case   orm   介绍   

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!

























Python 字符串内置函数(一)

标签:字符   make   频率   大小   字符串   sla   case   orm   介绍   

原文地址:https://www.cnblogs.com/shnuxiaoan/p/12934366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!