首页 > 其他分享 >字符串常用方法

字符串常用方法

时间:2023-03-30 20:57:02浏览次数:28  
标签:输出 常用 print str 字符串 world 方法 hello

1.capitalize() 方法:将字符串的首字母大写。

str = "hello, world"
print(str.capitalize())

输出:Hello, world

2.casefold() 方法:将字符串转换为小写并删除所有大小写特有的字符,使字符串可以比较。

str = "Hello, WORLD"
print(str.casefold())

输出:hello, world

3.center() 方法:将字符串居中对齐,并使用指定字符填充不足的空间。

str = "hello"
print(str.center(10, '*'))

输出:hello

4.count() 方法:返回字符串中指定子字符串的出现次数。

str = "hello, world"
print(str.count('l'))

输出:3

5.encode() 方法:将字符串转换为指定编码的字节序列。

str = "hello, world"
print(str.encode('utf-8'))

输出:b'hello, world'

6.endswith() 方法:判断字符串是否以指定子字符串结尾。

str = "hello, world"
print(str.endswith('ld'))

输出:True

7.expandtabs() 方法:将字符串中的制表符转换为指定数量的空格。

str = "hello,\tworld"
print(str.expandtabs(4))

输出:hello, world

8.find() 方法:查找字符串中指定子字符串的位置。

str = "hello, world"
print(str.find('l'))

输出:2

9.format() 方法:将参数格式化为指定格式的字符串。

x = 3
y = 5
print("The value of x is {} and y is {}".format(x, y))

输出:The value of x is 3 and y is 5

10.format_map() 方法:使用字典格式化字符串。

person = {'name': 'Alice', 'age': 25}
print("My name is {name} and I am {age} years old".format_map(person))

输出:My name is Alice and I am 25 years old

11.index() 方法:查找字符串中指定子字符串的位置。与 find() 方法相似,但子字符串不存在时会抛出异常。

str = "hello, world"
print(str.index('l'))

输出:2

12.isalnum() 方法:检查字符串是否只包含字母数字字符。

str = "hello123"
print(str.isalnum())

输出:True

13.isalpha() 方法:检查字符串是否只包含字母字符。

str = "hello"
print(str.isalpha())

输出:True

14.isdecimal() 方法:检查字符串是否只包含十进制数字字符。

str = "1234"
print(str.isdecimal())

输出:True

15.isdigit() 方法:检查字符串是否只包含数字字符。

str = "1234"
print(str.isdigit())

输出:True

16.isidentifier() 方法:检查字符串是否是一个有效的标识符。

str = "hello_world"
print(str.isidentifier())

输出:True

17.islower() 方法:检查字符串是否只包含小写字母。

str = "hello"
print(str.islower())

输出:True

18.isnumeric() 方法:检查字符串是否只包含数字字符。

str = "1234"
print(str.isnumeric())

输出:True

19.isprintable() 方法:检查字符串是否只包含可打印的字符。

str = "hello, world"
print(str.isprintable())

输出:True

20.isspace() 方法:检查字符串是否只包含空格字符。

str = "   "
print(str.isspace())

输出:True

21.istitle() 方法:检查字符串中的单词是否都以大写字母开头并且其余字母都是小写字母。

str = "Hello, World"
print(str.istitle())

输出:True

22.isupper() 方法:检查字符串是否只包含大写字母。

str = "HELLO"
print(str.isupper())

输出:True

23.join() 方法:将序列中的元素连接为一个字符串。

my_list = ["apple", "banana", "cherry"]
print("-".join(my_list))

输出:apple-banana-cherry

24.ljust() 方法:将字符串左对齐,并使用指定字符填充不足的空间。

str = "hello"
print(str.ljust(10, '*'))

输出:hello*****

25.lower() 方法:将字符串转换为小写。

str = "HELLO"
print(str.lower())

输出:hello

26.lstrip() 方法:删除字符串左侧的空格字符。

str = "   hello"
print(str.lstrip())

输出:hello

27.partition() 方法:查找子字符串,并将其拆分为三部分:前缀、子字符串、后缀。

str = "hello, world"
print(str.partition(','))

输出:('hello', ',', ' world')

28.replace() 方法:将字符串中的子字符串替换为另一个字符串。

str = "hello, world"
print(str.replace('o', '0'))

输出:hell0, w0rld

29.rfind() 方法:从右侧开始查找字符串中指定子字符串的位置。

str = "hello, world"
print(str.rfind('l'))

输出:10

30.rindex() 方法:从右侧开始查找字符串中指定子字符串的位置。与 rfind() 方法相似,但子字符串不存在时会抛出异常。

str = "hello, world"
print(str.rindex('l'))

输出:10

31.rjust() 方法:将字符串右对齐,并使用指定字符填充不足的空间。

str = "hello"
print(str.rjust(10, '*'))

输出:*****hello

32.rpartition() 方法:从右边开始查找子字符串,并将其拆分为三部分:前缀、子字符串、后缀。

str = "hello, world"
print(str.rpartition(','))

输出:('hello, world', '', '')

33.rsplit() 方法:将字符串从右侧开始拆分为指定数量的子字符串,并返回这些子字符串作为列表。

str = "hello, world"
print(str.rsplit(',', 1))

输出:['hello', ' world']

34.rstrip() 方法:从字符串的右侧删除空格字符。

str = "hello   "
print(str.rstrip())

输出:hello

35.split() 方法:将字符串拆分为指定数量的子字符串,并将这些子字符串作为列表返回。默认情况下,字符串根据空格进行拆分,但是可以指定分隔符。

str = "hello, world"
print(str.split(','))

输出:['hello', ' world']

36.splitlines() 方法:将字符串拆分为行,并将这些行作为列表返回。

str = "hello,\nworld"
print(str.splitlines())

输出:['hello,', 'world']

37.startswith() 方法:检查字符串是否以指定子字符串开头。

str = "hello, world"
print(str.startswith('he'))

输出:True

38.strip() 方法:从字符串两侧删除空格字符。

str = "   hello   "
print(str.strip())

输出:hello

39.swapcase() 方法:交换字符串中大写字母和小写字母的位置。

str = "Hello, World"
print(str.swapcase())

输出:hELLO, wORLD

40.title() 方法:将字符串中的每个单词的第一个字母变为大写。

str = "hello, world"
print(str.title())

输出:Hello, World

41.translate() 方法:使用指定的翻译表进行字符串转换。

str = "hello, world"
table = str.maketrans('hw', '12')
print(str.translate(table))

输出:12ello, 1orld

42.upper() 方法:将字符串转换为大写。

str = "hello"
print(str.upper())

输出:HELLO

43.zfill() 方法:将字符串填充到指定长度,并在左侧使用零字符('0')进行填充。

str = "123"
print(str.zfill(5))

输出:00123

标签:输出,常用,print,str,字符串,world,方法,hello
From: https://www.cnblogs.com/zouzhibin/p/17274244.html

相关文章

  • main 方法的执行过程
    参考https://www.cnblogs.com/max-home/p/12270183.htmlCS-NotesJavaGuide1、执行流程publicclassTest{publicstaticvoidmain(String[]args){Studentstudent=newStudent("abcd");student.sayName();}}classStudent{......
  • 3dMax 常用操作
    视点操作(1)Alt+W:      最大化视口切换;(2)鼠标滚轮:     向上/向下滚动—>放大/缩小视点;(3)鼠标滚轮:     按下时平移鼠标—>平移视点;(4)Alt+鼠标滚轮:按下时移动鼠标—>旋转视点;(5)鼠标左键:     点击选中要查看的物体后按Z键,最大化显......
  • 中文设置成粗体的方法
    在xml文件中使用android:textStyle="bold"可以将英文设置成粗体,但是不能将中文设置成粗体,将中文设置成粗体的方法是:TextViewtv=(TextView)findViewById(R.id.TextView01);TextPainttp=tv.getPaint();tp.setFakeBoldText(true);......
  • java.lang.String中的trim()方法的…
    String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗?一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱。首先我直接反编译String类,找到Trim()方法:publicstringTrim(){returnthis.TrimHelper(WhitespaceChars,2);}Trim......
  • C#文档转为Base64位字符串
    publicstaticstringDocumentToBase64Str(stringfileName){FileStreamfilestream=newFileStream(fileName,FileMode.Open);byte[]bt=newbyte[filestream.Length];//调用read读取方法filestr......
  • Linux系统把时间类型值转换为数值型的方法是什么?
    在实际工作中,我们往往会遇到各式各样的需求,今天老男孩教育小编给大家介绍一下,如何把时间类型值转换为数值类型,以下是详细的内容:1.取子串函数格式:substr(c,n1.n2)功能:取字符串C第n1个字符起的n2个字符.返回值类型是字符型.例:取姓名字符串中的姓.store"......
  • Linux系统中创建文件常用的方法!
    众所周知,在Windows系统中可以直接右键新建文件,而在Linux系统中,想要创建文件并非易事,需要通过执行命令来完成,那么Linux系统中创建文件常用的方法有哪些?本文为大家介绍一下Linux系统下创建文件的8种方法,快来了解一下吧。1、重定向符号>通常重定向符号可以创建一个0kb的......
  • 直线模组常用的驱动模式有哪些?
    直线模组是工业生产中经常用到的直线运动单元,其驱动方式有很多种,但常用的驱动方式通常有两种,一种是滚珠丝杆传动,另一种是同步带传动。滚珠丝杆传动方式的特点是重复定位精度较高,最高精度可达±0.005mm,并且负载一般也比较大,在工业生产中通常被应用于对精度有一定要求的生产过程中。......
  • DBSAT脚本快速收集方法
    DBSAT是Oracle官方提供的脚本,用于数据库的安全评估检查,用户可以放心下载使用。下载链接具体参见MOS:OracleDatabaseSecurityAssessmentTool(DBSAT)(DocID2138254.1)下面介绍DBSAT脚本快速收集方法:1.上传dbsat脚本并解压到指定目录2.使用dbsat采集必要信息3.生成报......
  • MySQL常用命令
    常用命令:https://blog.csdn.net/qq_38328378/article/details/80858073最详细的:https://blog.csdn.net/qq_34115899/article/details/81190461......