【python教程入门学习】六、python中字符串的常用方法_return a copy of the string converted to lowercase

python福利教程领取方式:
1、点赞+评论(勾选“同时转发”)
2、关注小编。并私信回复关键字【19】
(一定要私信哦~点击我的头像就能看到私信按钮了)
1.capitalize()方法

s = ‘jack and tom and jack’’’‘capitalize() 方法:Return a capitalized version of the string. —> 返回字符串的大写版本More specifically, make the first character have upper case and the rest lower case. —> 更具体的说,使第一个字符具有大写字母,其余字母为小写字母’’'print(s.capitalize())

2.title()方法

‘’‘title() 方法:Return a version of the string where each word is titlecased.–> 返回一个字符串,其中每个单词的首字母大写More specifically, words start with uppercased characters and all remainingcased characters have lower case. --> 更具体的说,每个单词以大写字母开始,其余都是小写字母’’'print(‘abc*abc’.title())

3.center()方法

‘’‘center() 方法:Return a centered string of length width.–>返回长度为宽度的居中字符串。Padding is done using the specified fill character (default is a space).–>填充是使用指定的填充字符完成的(默认为空格)’’‘print(‘abc’.center(9,’-’))

4.count()方法

‘’‘count() 方法:S.count(sub[, start[, end]]) -> intReturn the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation.查找子字符串 sub 在字符串中出现的次数,可选参数,开始 和 结束 可理解为切片’’'print(s.count(‘and’,1,3))

5.endswith()方法

‘’'endswith() 方法:S.endswith(suffix[, start[, end]]) -> bool Return True if S ends with the specified suffix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. suffix can also be a tuple of strings to try.如果S以指定后缀结尾,则返回True,否则返回False。可以指定起始和结束位置,后缀也可以是要尝试的字符串元组。 ‘’'print(‘tom and jack’.endswith(‘jack’,10))

6.find()方法

‘’‘find() 方法:S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.在字符串 S 中查找子字符串sub,如果找到,返回子字符串sub在字符串s中首次出现的索引,没有找到返回 -1参数start 和 end 可以理解为切片’’'print(‘hello world python’.find(‘world’,6))print(‘hello world python’.find(‘world’,6))

7.format()方法:

‘’‘format() 方法:S.format(*args, **kwargs) -> strReturn a formatted version of S, using substitutions from args and kwargs. The substitutions are identified by braces (’{’ and ‘}’).使用来自args和kwargs的替换返回S的格式化版本。替换用大括号(“{”和“}”)标识。’’'s_format = ‘我的名字叫{},我的年龄是{}’.format(‘jack’,19)print(s_format)s_format = ‘我的名字叫{name},我的年龄是{age},我的名字叫{name}’.format(name=‘jack’,age=19)print(s_format)s_format = ‘我的名字叫{0},我的年龄是{1},我的名字叫{0}’.format(‘jack’,19)print(s_format)

8.index()方法

‘’‘index() 方法:S.index(sub[, start[, end]]) -> intReturn the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Raises ValueError when the substring is not found.在字符串 s 中查找 子字符串 sub,在指定位置 start 和 end 之间查找,参数 start 和 end 可理解为切片,如果没有找到,则会抛出 ValueError错误。找到,返回最先出现的索引’’'s_index = 'return the lowest index in S where’print(s_index.index(‘the’))

9.lower()方法:

‘’‘lower() 方法:Return a copy of the string converted to lowercase.返回转换为小写的字符串副本。将字符串中的所有字符转换成小写字母’’'print(‘AbCdddEfgjKH’.lower())

10.lstrip()方法:

‘’‘lstrip() 方法: Return a copy of the string with leading whitespace removed. 返回一个删除前导(左侧)空白字符的副本。 If chars is given and not None, remove characters in chars instead. 如果这个字符chars是给定的,而不是None,则删除指定的字符,而不是空白字符’’‘print(’ abdde’.lstrip())print(’**abdcd’.lstrip(’’))

11.replace()方法:

‘’‘replace() 方法:Return a copy with all occurrences of substring old replaced by new.返回一个字符串的副本,使用新的子字符串替换旧的子字符串 count Maximum number of occurrences to replace. -1 (the default value) means replace all occurrences. 替换的次数,默认为-1 ,表示替换所有的子字符串 If the optional argument count is given, only the first count occurrences are replaced.如果参数count给定的指定次数。则替换指定的次数’’'print(‘abcabdabcabc’.replace(‘a’,‘A’,3))

12.split()方法:

‘’‘split()方法: Return a list of the words in the string, using sep as the delimiter string. 将字符串使用指定分隔符进行拆分,并返回一个list sep 用作拆分的分隔符 The delimiter according which to split the string. None (the default value) means split according to any whitespace, and discard empty strings from the result. 参数为空,默认使用空白字符进行拆分,返回的结果中抛弃空白字符 maxsplit 表示要执行的最大拆分 Maximum number of splits to do. -1 (the default value) means no limit. 表示要执行的最大拆分次数,-1 默认值,表示没有限制’’'print(‘Maximum number of splits to do’.split())

13.rstrip()方法

‘’‘rstrip() 方法:去掉字符串右边的字符,默认去掉空白字符,同 lstrip()方法’’‘print(‘abc***’.rstrip(’*’))’’‘startswith() 方法: S.startswith(prefix[, start[, end]]) -> boolReturn True if S starts with the specified prefix, False otherwise. With optional start, test S beginning at that position. With optional end, stop comparing S at that position. prefix can also be a tuple of strings to try. 如果以指定的字符串开头,则返回True,否则返回False. 同endswith()方法类似’’
'print(‘abc’.startswith(‘a’))

14.strip()方法

‘’‘strip()方法: 同rstrip() 和 lstrip() 方法。去掉字符串首尾的空白字符,或者指定字符’’’
print(’%abc%’.strip(’%’))

15.upper()方法

‘’‘upper()方法:Return a copy of the string converted to uppercase.将字符串中的所有字符转换成大写字母,并返回一个字符串副本 同lower()方法对应’’’
print(‘abcdaAKJFA’.upper())

python福利教程领取方式:
1、点赞+评论(勾选“同时转发”)
2、关注小编。并私信回复关键字【19】
(一定要私信哦~点击我的头像就能看到私信按钮了)

现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。

分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值