Python学习笔记:字符串(str)有关函数

本文介绍了Python中字符串(str)的多个重要函数,包括len(), max(), min(), enumerate(), str(), chr(), ord(), capitalize(), center(), count(), endswith(), startswith(), expandtabs(), find(), rfind(), index(), rindex(), isalnum(), isalpha(), isdigit(), isdecimal(), isspace(), islower(), isupper(), rjust(), ljust(), lower(), upper(), swapcase(), rstrip(), lstrip(), join(), replace(), split(), zfill()和title()。这些函数用于字符串的计数、遍历、转换、填充、查找等操作。" 105594304,9485897,C++对象模型与程序语义深度解析视频教程,"['C++编程', '对象模型', '内存管理', '程序语义', '构造函数']
摘要由CSDN通过智能技术生成

1.len()

len() 函数计算并返回字符串元素个数。(格式:len(string) )(string 代表字符串名字)

>>> S = 'abc'
>>> S
'abc'
>>> len(S)
3


2.max()、min()

max()、min()函数分别返回最大值和最小值,数字就返回数字,字母根据ASCII计算。(格式:max(string)、min(string)

>>> S = 'abc'
>>> S
'abc'
>>> max(S)
'c'
>>> min(S)
'a'
>>> S = '123'
>>> S
'123'
>>> max(S)
'3'
>>> min(S)
'1'


3.enumerate()

enumerate() 函数用来遍历序列的坐标和元素。(格式:enumerate(string)

>>> S = 'abcd'
>>> S
'abcd'
>>> for i,s in enumerate(S):
	print(i,s)

0 a
1 b
2 c
3 d
单纯返回的话不知道是什么东西。。。。

>>> S = 'abc'
>>> S
'abc'
>>> enumerate(S)
<enumerate object at 0x0000000003119168>



4.str() 

str() 函数将参数转变为字符串类型并返回副本,不改变原有类型。(格式:str(string)

>>> T = [1,2,3]
>>> T
[1, 2, 3]
>>> type(T)
<class 'list'>
>>> str(T)
'[1, 2, 3]'
>>> type(T)
<class 'list'>
>>> type(str(T))
<class 'str'>


5.chr() 、 ord()

chr() 函数可将传入的单个数字转化成对应的ASCII的字母返回。

ord() 函数可将传入的单个字母转化成对应的ASCII的数字返回。

>>> chr(65)
'A'
>>> ord('a')
97
>>> ord('abc')<span style="white-space:pre">		</span>#传入多个字母出现异常
Traceback (most recent call last):
  File "<pyshell#383>", line 1, in <module>
    ord('abc')
TypeError: ord() expected a character, but string of length 3 found



6.capitalize()

capitalize() 函数将字符串的第一个字母大写并返回副本,不改变原有数据。(格式:string.capitalize()

>>> S = 'abc'
>>> S
'abc'
>>> S.capitalize()
'Abc'
>>> S
'abc'



7.center()

center() 函数将使字符串居中输出,两边用空格填充,不改变原有数据。(格式:string.center(width)  )(width为参数,表示总宽度 )

>>> S = 'abc'
>>> S
'abc'
>>> S.center(10)	
'   abc    '		#宽度10  
>>> S
'abc'
>>> S.center(2)
'abc'			#若设置的宽度小于元字符串,没什么变化。
>>> S
'abc'



8.count()

c

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值