python中的字符串

本文详细介绍了Python字符串的基本概念和常见操作,包括定义方法、字符串方法如capitalize()、center()、count()等,并通过实例展示了如何使用这些方法进行字符串处理。此外,还提及了字符串的编码与解码、格式化以及大小写转换等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.什么是字符串

2.定义方法

3.常见的方法


1.什么是字符串

字符串由很多有效字符,如数字、字母、下划线组成的一串字符

2.定义方法

1.由双引号,单引号,三双引号,三单引号定义

2.s = str()

3.常见的方法

(通过调用help文档查看)

['capitalize','casefold','center','count','encode','endswith','expandtabs','find','format','format_map','index','isalnum','isalpha','isascii',
'isdecimal','isdigit','isidentifier','islower','isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip','maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
1. capitalize() ---- 格式化字符串的方法 将字符串的首字母转换为大写
>>> s = "hahahaha"
>>> s 'hahahaha' 
>>> s.capitalize() 
'Hahahaha'

2.center(width, fillchar=' ') ---- 字符串长度居中

>>> s = "hahahaha"
>>> s.center(30) 
' hahahaha ' 
>>> s.center(30,"*") 
'***********hahahaha***********'

3.count() ---- 统计字符或者字符串出现的次数

>>> s = "hahahaha"
>>> s 'hahahaha' 
>>> s.count("h") 
4
>>> s.count("ha") 
4
>>> s.count("haha") 
2

4.endswith() ---- 判断字符串是不是以XXX结尾

5.startswith() ---- 判断字符串是不是以XXX开头

6.index() ---- 查询字符或者字符串在字符串中出现的第一次的位置,如果没有则抛出异常

7.rindex() ---- 从右往左找
8.find() -------- 查询字符或者字符串在字符串中出现的第一次的位置,如果没有则会返回 -1
9.rfind() ------ 从右往左找
10.encode() ---- 将字符串转换为字节数据的方法 如果要将字节数据转换为字符数据 decode() (这个方法是 字节里面的方法 )
>>> s = "hahahaha"
>>> s.encode() 
b'hahahaha' 
>>> d = s.encode() 
>>> d 
b'hahahaha' 
>>> type(d) 
<class 'bytes'> 
>>> d.decode() 
'hahahaha' 
>>> ss = d.decode() 
>>> type(ss) 
<class 'str'>

11.format() ----- 格式化字符串 a = 3 b =2 print("a = {} ,b ={}".format(a,b))

12.islower() ------ 判断字母是否全为小写

13.isupper()------ 判断字母是否全为大写
>>> s = "hahahaha"
>>> s 
'hahahaha' 
>>> s.islower() 
True 
>>> s.isupper() 
False

14.istitle() ---- 判断是否为标题

>>> ss = "This is ..." 
>>> ss.istitle() 
False
>>> ss = "This Is .."
>>> ss.istitle() 
True

15.isspace() --- 判断是否为空格位

16.isdigit() ---- 判断是否为数字

>>> s = "hahahaha"
>>> s 
'hahahaha' 
>>> s.isdigit() 
False 
>>> ss = "1234" 
>>> ss.isdigit() 
True

17.isalnum() ----- 不是判断是否位数字,判断是否为有效字符(数字、字母、下划线)

>>> s = "hahahaha"
>>> ss = "$$$$$" 
>>> ss.isalnum() 
False 
>>> s 
'hahahaha' 
>>> s.isalnum() 
True

18.isalpha() ---- 是否全为字母

>>> s = "hahahaha"
>>> s.isalpha() 
True

19.title() ----- 将字符串转换为标题

>>> ss = "this is a dog" 
>>> ss.title() 
'This Is A Dog'

20.lower() ----- 将字符全部转换为小写

21.upper()------- 将字符全部转换为大写
s = "hahahaha"
>>> s 
'hahahaha' 
>>> s.upper() 
'HAHAHAHA'

22.split() ---- 将字符串按照特定的格式进行分割,返回值是一个列表

>>> s = "hahahaha"
>>> ss = "this is a dog"
>>> ss 
'this is a dog' 
>>> ss.split(" ") 
['this', 'is', 'a', 'dog'] 
>>> ss 
'this is a dog' 
>>> ss.split("s") 
['thi', ' i', ' a dog']

23.join() --- 按照特定的符号将一个可迭代对象拼接字符串(注意的是这个方法不是列表或者其他容器的方法)

>>> ls = ["a","b","c"] 
>>> " ".join(ls) 
'a b c' 
>>> "*".join(ls) 
'a*b*c'

24.strip() ----- 清除字符串两侧空格

25.lstrip() ------- 清除字符串左侧空格
26.rstrip() ------- 清除字符串右侧空格
>>> ss = "    name    " 
>>> ss.strip() 
'name'

27.replace(old,new) ----- 用新的字符替换旧的字符

>>> s = "hahahaha" 
>>> s.replace("h","H") 
'HaHaHaHa'

28.ljust() ------- 左对齐

29.rjust()------ 右对齐
>>> s = "hahahaha"
>>> s.ljust(30) 
'hahahaha            '
>>> s.rjust(30) 
'            hahahaha' 
>>> s.rjust(30,"*") 
'**********************hahahaha'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值