python字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | test = 'abcdefghijklmnopqrstuvwxyz' print (test) print (test[ 1 : 5 ]) #可以切片 print (test.capitalize()) #首字母大写 print (test.count( 'a' )) #计数 print (test.center( 50 , '-' )) #美观打印 print (test.encode( 'utf-8' )) #转码 print (test.endswith( 'yz' )) #判断结尾 print (test.endswith( 'zz' )) #判断结尾 test = 'a\tbcdefghijklmnopqrstuvwxyz' #\t是tab键 print (test.expandtabs(tabsize = 5 )) #定义tab键的长度 test = 'abcdefghijklmnopqrstuvwxyz' print (test.find( 'i' )) #找到位置 test = "My name is {name},I'm {age} old." print (test. format (name = 'jack' , age = 20 )) #格式化输出 print (test.format_map({ 'name' : 'rose' , 'age' : 18 })) #另一种格式化输出 print ( 'abc123' .isalnum()) #是否数字字母 print ( 'abc123!@#' .isalnum())#不包含符号 print ( 'abcABC' .isalpha()) #是否字母 print ( 'abcABC123' .isalpha()) #是否字母不含数字 print ( '1a' .isdecimal()) #是否10进制 print ( '10' .isdigit()) #是否数字 print ( 'aa' .isidentifier()) #是否是合法标识符 print ( 'a' .islower()) #是否小写 print ( '123a' .isnumeric()) #是否纯数字 print ( ' ' .isspace()) #是否空格 print ( 'My name is jack' .istitle()) #是否首字母全大写 print ( 'My name is jack' .isprintable()) #是否可以打印tty drive 不可以打印 print ( 'My name is jack' .isupper()) #是否全大写 print ( 'My name is jack' .join( '------' )) #分割组合 分割joni里面的,把前面的加进去 print ( '+' .join([ '1' , '2' , '3' ])) #1+2+3 print (test.ljust( 50 , '*' )) #用*在后面补齐50位 print (test.rjust( 50 , '*' )) #用*在前面补齐50位 print ( 'jack' .upper()) #全换大写 print ( 'Jack' .lower()) #全换小写 print ( '\n test \n' ) #\n是回车 print ( '--' ) print ( '\n test \n' .rstrip()) #去掉右边的空格和回车 print ( '--' ) print ( '\n test \n' .lstrip()) #去掉左边的空格和回车 print ( '--' ) print ( '\n test \n' .split()) #去掉两边的空格和回车 p = str .maketrans( 'abcdefg' , '1234567' ) #明文替换 print ( 'jack' .translate(p)) print ( 'jack adam' .replace( 'a' , 'A' )) #替换 print ( 'jack adam' .replace( 'a' , 'A' , 2 )) #替换几个 print ( 'jack adam' .find( 'a' )) #查找一个字符的位置 print ( 'jack adam' .rfind( 'a' )) #查找某字符的最右的位置 print ( 'jack adam' .split()) # 用空格分成字符串 print ( 'jack adam' .split( 'a' )) # 用a分成字符串 a就没了 print ( 'jack \nadam' .splitlines()) # 用换行分成字符串 print ( 'Jack Adam' .swapcase()) #大小写转换 print ( 'jack adam' .title()) #首字母大写 print ( 'jack adam' .zfill( 50 )) #用0填充50位 |
作者:上官飞鸿
出处:https://www.cnblogs.com/jackadam/p/7843765.html
版权:本作品采用「知识共享-署名-非商业性-禁止演绎(CC-BY-NC-ND)」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现