python 基础之字符串方法

字符串

1
print('chenxi'*8)

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
chenxichenxichenxichenxichenxichenxichenxichenxi
 
Process finished with exit code 0

  字符串切片打印处理

1
print('chenxi'[2:])

  测试

1
2
D:\python\python.exe D:/untitled/dir/for.py
enxi

  判断字符串里有没有包含对应得字符

1
2
print('x'in 'chenxi')
print('f'in 'chenxi')

  测试

1
2
True
False

  字符串拼接,不建议使用这种方法

1
2
3
4
a='cx'
b='zrd'
c=a+b
print(c)

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
cxzrd
 
Process finished with exit code 0

  字符串拼接

1
2
3
4
a='cx'
b='zrd'
c=''.join([a,b])
print(c)

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
cxzrd
 
Process finished with exit code 0

  字符串拼接

1
2
3
4
5
a='cx'
b='zrd'
bb='haha'
c='-----'.join([a,b,bb])
print(c)

  测试

1
2
D:\python\python.exe D:/untitled/dir/for.py
cx-----zrd-----haha

  字符串之统计关键字个数

1
2
dis = 'hebei tianjing guanzhou'
print(dis.count('i'))

  测试

1
2
D:\python\python.exe D:/untitled/dir/for.py
3

  字符串之修改首字母为大写

1
2
dis = 'hebei tianjing guanzhou'
print(dis.capitalize())

  测试

1
2
D:\python\python.exe D:/untitled/dir/for.py
Hebei tianjing guanzhou

  一共打印50字符,不够用指定字符去补,原先字符串内容居中

1
2
dis = 'hebei tianjing guanzhou'
print(dis.center(50,'#'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
#############hebei tianjing guanzhou##############
 
Process finished with exit code 0

  判断字符串是不是以什么结尾的

1
2
3
dis = 'hebei tianjing guanzhou'
print(dis.endswith('u'))
print(dis.endswith('i'))

  测试

1
2
3
4
5
D:\python\python.exe D:/untitled/dir/for.py
True
False
 
Process finished with exit code 0

  判断字符串是不是以什么开头的

1
2
dis = 'hebei tianjing guanzhou'
print(dis.startswith('he'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
True
 
Process finished with exit code 0

  修改字符串里tab的默认空格数量\t 表示tab键

1
2
dis = 'he\tbei tianjing guanzhou'
print(dis.expandtabs(tabsize=10))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
he        bei tianjing guanzhou
 
Process finished with exit code 0

  找到字符串里第一个元素定返回索引值

1
2
dis = 'hebei tianjing guanzhou'
print(dis.find('a'))

  测试

1
2
D:\python\python.exe D:/untitled/dir/for.py
8

  将字符串变量赋值打印

1
2
dis = 'hebei tianjing guanzhou {name}'
print(dis.format(name='zrd'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd
 
Process finished with exit code 0

  以字典方式批量给字符串赋值

1
2
dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.format_map({'name':'zrd','age':22}))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
hebei tianjing guanzhou zrd ll 22
 
Process finished with exit code 0

  查字符串里关键字并返回索引值,字符串里没有关键字报错

1
2
3
dis = 'hebei tianjing guanzhou {name} ll {age}'
print(dis.index('i'))
print(dis.index('d'))

 测试

1
2
3
4
5
6
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

  判断字符串是否包含特殊字符

1
2
3
4
5
print('test564'.isalnum() )
print('tygc*'.isalnum())
print('reswd'.isalnum())
print('7890'.isalnum())
print('宋氏家族'.isalnum())

  测试

1
2
3
4
5
6
D:\python\python.exe D:/untitled/dir/for.py
True
False
True
True
True

  判断字符串像不像十进制的数字

1
2
3
4
print("3455".isdecimal())
print("程序".isdecimal())
print("AF09".isdecimal())
print("hevb".isdecimal())

  测试

1
2
3
4
5
D:\python\python.exe D:/untitled/dir/for.py
True
False
False
False

  判断字符串是否包含一个非法字符

1
2
print("dtfghvh".isidentifier())
print("1233tyghgvh".isidentifier())  #非法字符

  测试

1
2
3
D:\python\python.exe D:/untitled/dir/for.py
True
False

  判断字符是不是空格

1
print(' '.isspace())

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
True
 
Process finished with exit code 0

  判断字符串是不是标题格式

1
2
print('My Ch'.istitle())
print('My ch'.istitle())

  测试

1
2
True
False

  字符串里所有大写改成小写

1
print('My Ch'.lower())

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
my ch
 
Process finished with exit code 0

  字符串所有小写该大写

1
print('My Ch'.upper())

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
MY CH
 
Process finished with exit code 0

  字符串中大小写翻转

1
print('My Ch'.swapcase())

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
mY cH
 
Process finished with exit code 0

  在字符串后面以特定字符补够特定数量

1
print('My Ch'.ljust(50'*'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
My Ch*********************************************
 
Process finished with exit code 0

  在字符串前面以特定字符补够特定数量

1
print('My Ch'.rjust(50,'*'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
*********************************************My Ch
 
Process finished with exit code 0

  将字符串前后空格去掉

1
print('       My Ch'.strip())

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
My Ch
 
Process finished with exit code 0

  字符串内容替换

1
print('chenxi ffff'.replace('ffff','zrd'))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd
 
Process finished with exit code 0

  字符串内容替换的次数控制

1
2
3
print('chenxi ffff'.replace('ffff','zrd'))
print('chenxi ffff  tygdf'.replace('ff','zrd'))
print('chenxi ffff  tygdf'.replace('ffff','zrd',1))

  测试

1
2
3
4
5
6
D:\python\python.exe D:/untitled/dir/for.py
chenxi zrd
chenxi zrdzrd  tygdf
chenxi zrd  tygdf
 
Process finished with exit code 0

  查字符串最后一个关键字在第几个索引

1
print('chenxi cx xlc'.rfind('x'))

 测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
10
 
Process finished with exit code 0

  将字符串以空格为分隔符,分成列表

1
print('chenxi cx xlc'.split(' '))

  测试

1
2
3
4
D:\python\python.exe D:/untitled/dir/for.py
['chenxi', 'cx', 'xlc']
 
Process finished with exit code 0

  

 

posted @   烟雨楼台,行云流水  阅读(181)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示