python 基础之字符串方法
2024-09-06 23:46:05
字符串
print('chenxi'*8)
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxichenxichenxichenxichenxichenxichenxichenxi Process finished with exit code 0
字符串切片打印处理
print('chenxi'[2:])
测试
D:\python\python.exe D:/untitled/dir/for.py
enxi
判断字符串里有没有包含对应得字符
print('x'in 'chenxi')
print('f'in 'chenxi')
测试
True
False
字符串拼接,不建议使用这种方法
a='cx'
b='zrd'
c=a+b
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
cxzrd Process finished with exit code 0
字符串拼接
a='cx'
b='zrd'
c=''.join([a,b])
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
cxzrd Process finished with exit code 0
字符串拼接
a='cx'
b='zrd'
bb='haha'
c='-----'.join([a,b,bb])
print(c)
测试
D:\python\python.exe D:/untitled/dir/for.py
cx-----zrd-----haha
字符串之统计关键字个数
dis = 'hebei tianjing guanzhou'
print(dis.count('i'))
测试
D:\python\python.exe D:/untitled/dir/for.py
3
字符串之修改首字母为大写
dis = 'hebei tianjing guanzhou'
print(dis.capitalize())
测试
D:\python\python.exe D:/untitled/dir/for.py
Hebei tianjing guanzhou
一共打印50字符,不够用指定字符去补,原先字符串内容居中
dis = 'hebei tianjing guanzhou'
print(dis.center(50,'#'))
测试
D:\python\python.exe D:/untitled/dir/for.py
#############hebei tianjing guanzhou############## Process finished with exit code 0
判断字符串是不是以什么结尾的
dis = 'hebei tianjing guanzhou'
print(dis.endswith('u'))
print(dis.endswith('i'))
测试
D:\python\python.exe D:/untitled/dir/for.py
True
False Process finished with exit code 0
判断字符串是不是以什么开头的
dis = 'hebei tianjing guanzhou'
print(dis.startswith('he'))
测试
D:\python\python.exe D:/untitled/dir/for.py
True Process finished with exit code 0
修改字符串里tab的默认空格数量\t 表示tab键
dis = 'he\tbei tianjing guanzhou'
print(dis.expandtabs(tabsize=10))
测试
D:\python\python.exe D:/untitled/dir/for.py
he bei tianjing guanzhou Process finished with exit code 0
找到字符串里第一个元素定返回索引值
dis = 'hebei tianjing guanzhou'
print(dis.find('a'))
测试
D:\python\python.exe D:/untitled/dir/for.py
8
将字符串变量赋值打印
dis = 'hebei tianjing guanzhou {name}'
print(dis.format(name='zrd'))
测试
D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd Process finished with exit code 0
以字典方式批量给字符串赋值
dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.format_map({'name':'zrd','age':22}))
测试
D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd ll 22 Process finished with exit code 0
查字符串里关键字并返回索引值,字符串里没有关键字报错
dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.index('i'))
print(dis.index('d'))
测试
D:\python\python.exe D:/untitled/dir/for.py
Traceback (most recent call last):
File "D:/untitled/dir/for.py", line 170, in <module>
print(dis.index('d'))
ValueError: substring not found
4
判断字符串是否包含特殊字符
print('test564'.isalnum() )
print('tygc*'.isalnum())
print('reswd'.isalnum())
print('7890'.isalnum())
print('宋氏家族'.isalnum())
测试
D:\python\python.exe D:/untitled/dir/for.py
True
False
True
True
True
判断字符串像不像十进制的数字
print("3455".isdecimal())
print("程序".isdecimal())
print("AF09".isdecimal())
print("hevb".isdecimal())
测试
D:\python\python.exe D:/untitled/dir/for.py
True
False
False
False
判断字符串是否包含一个非法字符
print("dtfghvh".isidentifier())
print("1233tyghgvh".isidentifier()) #非法字符
测试
D:\python\python.exe D:/untitled/dir/for.py
True
False
判断字符是不是空格
print(' '.isspace())
测试
D:\python\python.exe D:/untitled/dir/for.py
True Process finished with exit code 0
判断字符串是不是标题格式
print('My Ch'.istitle())
print('My ch'.istitle())
测试
True
False
字符串里所有大写改成小写
print('My Ch'.lower())
测试
D:\python\python.exe D:/untitled/dir/for.py
my ch Process finished with exit code 0
字符串所有小写该大写
print('My Ch'.upper())
测试
D:\python\python.exe D:/untitled/dir/for.py
MY CH Process finished with exit code 0
字符串中大小写翻转
print('My Ch'.swapcase())
测试
D:\python\python.exe D:/untitled/dir/for.py
mY cH Process finished with exit code 0
在字符串后面以特定字符补够特定数量
print('My Ch'.ljust(50,'*'))
测试
D:\python\python.exe D:/untitled/dir/for.py
My Ch********************************************* Process finished with exit code 0
在字符串前面以特定字符补够特定数量
print('My Ch'.rjust(50,'*'))
测试
D:\python\python.exe D:/untitled/dir/for.py
*********************************************My Ch Process finished with exit code 0
将字符串前后空格去掉
print(' My Ch'.strip())
测试
D:\python\python.exe D:/untitled/dir/for.py
My Ch Process finished with exit code 0
字符串内容替换
print('chenxi ffff'.replace('ffff','zrd'))
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd Process finished with exit code 0
字符串内容替换的次数控制
print('chenxi ffff'.replace('ffff','zrd'))
print('chenxi ffff tygdf'.replace('ff','zrd'))
print('chenxi ffff tygdf'.replace('ffff','zrd',1))
测试
D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd
chenxi zrdzrd tygdf
chenxi zrd tygdf Process finished with exit code 0
查字符串最后一个关键字在第几个索引
print('chenxi cx xlc'.rfind('x'))
测试
D:\python\python.exe D:/untitled/dir/for.py
10 Process finished with exit code 0
将字符串以空格为分隔符,分成列表
print('chenxi cx xlc'.split(' '))
测试
D:\python\python.exe D:/untitled/dir/for.py
['chenxi', 'cx', 'xlc'] Process finished with exit code 0
最新文章
- tyvj1144 股票
- win7安装Linux
- leetcode 日记 162. Find Peak Element java python
- Java基础之常用类
- android Gui系统之SurfaceFlinger(5)---Vsync(2)
- RBAC权限设计实例
- HTML DOM基础知识
- Windows server 2008 R2远程桌面终端连接数的破解
- IIS URL重写找不到页面 (URLRewriter.dll伪静态)
- 发送带有认证信息的HTTP请求并取回响应
- java高精度数组
- android 按字母搜索
- System.Threading.ThreadStateException
- js基础和工具库
- Vue和Bootstrap的整合之路
- 个人作业1——四则运算题目生成程序(基于C++)
- C#隐式转换和显示转换举例--C#基础
- 《java入门第一季》之类String类小案例
- 【原】Java学习笔记003 - 数据类型
- python读文件指定行的数据