字符串操作
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 | #Author:Kevin_hou #字符串操作 name = " My name is kevin.hou and I am 30 years old" name1 = "123456" print (name.__add__( ' !!!' )) #在字符串后面添加值 print (name.count( 'a' )) #计算字符串中a的数量 print (name.center( 50 , '*' )) #使字符串居中,左右添加* print (name.endswith( '30' )) #判断是否已字符串结束,不是返回False print (name.endswith( 'old' )) #是,返回True print (name. format (name = 'kevin.hou' ,year = 30 )) # print (name.find( 'I' )) #查找字符串’I‘,输出位置值 print ( 'my name is' .istitle()) #首字母小写判定为不是标题,输出False print ( 'My Name Is' .istitle()) #首字母大写判定为标题,输出为True print ( 'abc' .islower()) #判断是否为小写;是,True;非,False print ( 'ABC' .islower()) #False print ( '321' .islower()) #False print (name.isdigit()) #判断name是否为数字变量,不是,则返回False;是,则返回True print (name1.isdigit( )) # print ( 'abc123' .isalnum()) #判断字符串变量是否为字母或者数字;是,输出True;非,输出False print ( ',*_<' .isalnum()) print ( 'a*_<' .isalnum()) print ( 'a 2B' .isidentifier()) #判断是否为1个合法标识符;False print ( 'a_2B' .isidentifier()) #True print ( 'a2B' .isidentifier()) #True print ( 'a' .isidentifier()) #True print ( ',' .join([ '1' , '2' , '3' ])) #在字符串输出间添加逗号;输出为1,2,3 print (name.ljust( 50 , '-' )) #输出:My name is kevin.hou and I am 30 years old------- print (name.rjust( 50 , '*' )) #输出: ******* My name is kevin.hou and I am 30 years old print ( 'KEVIN' .lower()) #大写变小写;输出:kevin print ( 'kevin' .upper()) #小写变大写;输出:KEVIN print ( 'kevin\n' .lstrip()) #省去中间输出的换行 print ( 'kevin' .lstrip()) print ( 'KEVIN.HOU' .rfind( 'o' )) #在字符串中找到'o';找到,输出数组值;找不到,输出-1。输出:-1 print ( 'KEVIN.HOU' .rfind( 'x' )) #输出:-1 print ( 'KEVIN.HOU' .rfind( 'O' )) #输出:7 print ( 'KEVIN.HOU' .replace( 'KEVIN' , 'kevin' , 5 )) #替换值;输出:kevin.HOU print ( '1+2+3' .split( '+' )) #按照加号分割。输出:['1','2','3'] |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律