python 3 ---字符串方法使用整理

简介:

一、编码部分(结合执行结果进行理解)

name = "my \tname is {name} and i am {year} old"
print(name.capitalize())

首字母大写

print(name.center(50,"-"))
#打印50个字符,不够的用-补齐,并将chenhl放在中间。
print(name.encode())
#将字符串转成二进制,在python中0开头表示8进制,0x或者0X表示16进制,0b或者0B表示二进制。
print(name.endswith("hl"))
#判断字符串以什么结尾
print(name.expandtabs(tabsize=10))
#自动在字符串中的tab键处补上空格
print(name.format(name='chenhl',year='30'))
print(name.format_map({'name':'chenhl','year':'20'}))
#格式化输入,map 用于字典中
print(name.find("n"))
#获取n的索引,即位置,字符串可以切片
print(name.isalnum())
#判断变量是否是阿拉伯数字+阿拉伯字符,包含英文字符和0---9数字。是返回true,非则相反。
print('name'.isalpha())
#判断变量是否是纯英文字符,是返回true,非则相反。
print(name.isdecimal())
print('1.23'.isdecimal())
print('11'.isdecimal())
print('1B'.isdecimal())
#判断是否是十进制
print(name.isdigit())
#判断是不是整数
print('name'.isidentifier())
#判断是不是一个合法的标识符
print(name.islower())
#判断是不是小写
print(name.isnumeric())
print('12'.isnumeric())
print('1.2'.isnumeric())
#判断是不是只有数字
print('my name is'.istitle())
print('My Name Is'.istitle())
#判断是不是首字母大写
print('my name is '.isprintable())
#判断是不是可以打印的文件,tty的文件会返回非。
print('my future'.isupper())
#判断是不是都是大写
print(','.join(['1','2','3']))
#将列表的内容转成字符串,或将join内容当成一条指令交给os执行。
print(name.ljust(50,''))
#保证字符串的长度,不够用
在右侧补齐。
print(name.rjust(50,'-'))
#保证字符串长度,不够用-在左侧补齐。
print('ABCD'.lower())
#把大写变成小写
print('abcd'.upper())
#把小写变成大写
print(' abcd'.lstrip())
print('\nabcd'.lstrip())
#去掉左侧的空格或回车。rstrip,去掉右侧的空格或回车,strip去掉两侧的。
p = str.maketrans('abcdef','123456')
print('chenhl'.translate(p))
#将字符串换成后面数字对应的值
print('chenhl'.replace('h','H',1))
#将h替换成大写H,count替换几个
print('chenhl'.rfind('h'))
#找到值得最后一个位置
print('ch en hl'.split())
print('ch en hl'.split('h'))
#把字符串按照空格或固定字符转成列表
print('che\nhl'.splitlines())
#把字符串按照换行转成列表,特别是不同系统的换行
print('chenhl'.startswith('c'))
#判断以什么开头的字符串
print('Hello World'.swapcase())
#将大写的首字母转换成小写
print('hello world'.title())
#将首字母变成大写
print('hello world'.zfill(20))
#用零补位,

二、执行结果
My name is {name} and i am {year} old
------my name is {name} and i am {year} old------
b'my \tname is {name} and i am {year} old'
False
my name is {name} and i am {year} old
my name is chenhl and i am 30 old
my name is chenhl and i am 20 old
4
False
True
False
False
True
False
False
True
True
False
True
False
False
True
True
False
1,2,3
my name is {name} and i am {year} old****
------------my name is {name} and i am {year} old
abcd
ABCD
abcd
abcd
3h5nhl
cHenhl
4
['ch', 'en', 'hl']
['c', ' en ', 'l']
['che', 'hl']
True
hELLO wORLD
Hello World
000000000hello world



      本文转自Mr_chen  51CTO博客,原文链接:http://blog.51cto.com/metis/2051065,如需转载请自行联系原作者




相关文章
|
9月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
587 1
|
10月前
|
机器学习/深度学习 数据采集 数据挖掘
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
338 2
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
473 100
|
9月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
626 99
|
9月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
9月前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
9月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
9月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
9月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
1417 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
8月前
|
存储 Java 索引
(Python基础)新时代语言!一起学习Python吧!(二):字符编码由来;Python字符串、字符串格式化;list集合和tuple元组区别
字符编码 我们要清楚,计算机最开始的表达都是由二进制而来 我们要想通过二进制来表示我们熟知的字符看看以下的变化 例如: 1 的二进制编码为 0000 0001 我们通过A这个字符,让其在计算机内部存储(现如今,A 字符在地址通常表示为65) 现在拿A举例: 在计算机内部 A字符,它本身表示为 65这个数,在计算机底层会转为二进制码 也意味着A字符在底层表示为 1000001 通过这样的字符表示进行转换,逐步发展为拥有127个字符的编码存储到计算机中,这个编码表也被称为ASCII编码。 但随时代变迁,ASCII编码逐渐暴露短板,全球有上百种语言,光是ASCII编码并不能够满足需求
339 4

推荐镜像

更多