四、python中的字符串

本文详细介绍了字符串的基本操作,包括转换、组合、切片、查找、替换、切割等,并提供了丰富的示例代码,帮助读者掌握字符串处理技巧。

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

字符串常见操作

转换字符串

str()

字符串组合

+

下标和切片

左闭右开
正序 [0:]
逆序 [::-1]

长度

len()

个数

count()

查找

find()
rfind()
index()
rindex()

替换

replace()

字符串切割

split() 返回列表
splitline() 返回列表
partition() 返回元组
rpartition()
注意区别:一个返回列表,一个返回元组

判断开头/结尾

startswith()
endswith()

大小写转换

title() 所有单词首字母大写
capitalize() 字符串的首字母大写
upper() / lower()

对齐

ljust()
rjust()
center()

删除空格

lstrip() 删除左边空格
rstrip() 删除右边空格
strip() 删除全部空格

判断字母/数字

isalpha()
isdigit()
isalnum()

组合列表

join() 将列表组合为字符串

问题

test = “aa bb \t cc \t dd”
如何删除test中的空格和\t ?

split()

如何再组合成新的字符串 ?

join()

name = "zhangsan"
#获取长度
print(len(name))

age = 18
#转换为转字符串
print(type(str(age)))

#字符串组合
a = "zhang"
b = "san"
c = a + b
print(c)

#打印 '===zhangsan==='
print("===%s==="%(a+b))

#下标和切片(左闭右开)
print(name[0:])   #正序
print(name[::-1]) #逆序

#常见操作
#查找字符串位置 find() index()
my_str = "   flying in the world together world  "
print(my_str.find("world"))  #查找字符串位置(从左到右)找不到返回-1
print(my_str.find("is"))
print(my_str.rfind("world")) #从右开始查找

print(my_str.index("world"))  #找不到返回异常
print(my_str.rindex("world"))

#个数 count()
print(my_str.count("world")) #返回单词个数

#替换 replace()
print(my_str.replace("world","WORLD")) #原字符串没有变化

#切割 split() 返回的是列表
print(my_str.split(" "))

#按照换行符切割 splitlines()  返回列表
test_str = "hello\nworld\n"
print(test_str.splitlines())

#字符串切割 partition()/rpartition() 返回元组
print(my_str.partition("world"))
print(my_str.rpartition("world"))



#首字母大写 title()
print(my_str.title())

#字符串首字母大写 capitalize()
print(my_str.capitalize())

#字符串中大写字符转变为小写字符 upper()/lower()
print(my_str.lower())
print(my_str.upper())

#判断以xxxx开头或者结尾 startswith()   endswith()  返回值为Ture
filename = 'helloworld.txt'
print(filename.startswith("hello"))
print(filename.endswith(".txt"))

#对齐(左对齐/右对齐/居中)ljust()/rjust()/center()
print(my_str.ljust(50))
print(my_str.rjust(50))
print(my_str.center(50))


#删除空格 lstrip()/rstrip()/strip()
print(my_str.lstrip())
print(my_str.rstrip())
print(my_str.strip())

#判断是否是字母/数字/数字和字母组合 isalpha()/isdigit()/isalnum()
print(my_str.isalpha())
print(my_str.isdigit())
print(my_str.isalnum())

#组合列表形成一个字符串 join()
a = ["aaa","bbb","ccc"]
b = "="
print(b.join(a))
c = " "
print(c.join(a))

'''
test = "aa bb \t cc \t dd"
如何删除test中的空格和\t?
如何再组合成新的字符串?
'''
test_str = "aa bb \t cc \t dd"
print(test_str)
print(test_str.split())

result = test_str.split()
c = ""
print(c.join(result))


Python中的字符串是一种不可变的序列类型,用于表示文本数据。字符串可以通过单引号(')、双引号(")或三引号('''或""")来创建。三引号字符串可以跨越多行,常用于多行字符串和注释。 字符串Python中是序列的一种,所以它支持一些通用的序列操作,比如索引、切片、乘法和成员资格测试等。 下面是字符串的一些常见操作: 1. 索引与切片:通过索引可以访问字符串中的特定字符,通过切片可以获取字符串的一部分。 ```python s = "Hello, world!" print(s[0]) # 输出 'H' print(s[1:5]) # 输出 'ello' ``` 2. 字符串连接:可以使用加号(+)来连接两个字符串。 ```python s1 = "Hello" s2 = "world" print(s1 + ", " + s2) # 输出 'Hello, world' ``` 3. 重复:使用乘法操作符(*)可以重复字符串。 ```python print("Python" * 3) # 输出 'PythonPythonPython' ``` 4. 成员资格测试:使用in和not in来检查某个字符串是否包含在另一个字符串中。 ```python print('H' in "Hello") # 输出 True print('z' not in "Python") # 输出 True ``` 5. 转义字符:在字符串中可以使用反斜杠(\)来引入特殊字符,如换行(\n)、制表符(\t)等。 ```python print("Hello\nPython") # 输出 'Hello' 后跟一个换行,然后是 'Python' ``` 6. 原始字符串:在字符串前加上前缀r或R表示原始字符串,它不会处理字符串中的转义字符。 ```python print(r"\n") # 输出 '\n' 而不是换行 ``` 7. 字符串方法:Python提供了许多字符串方法,例如upper(), lower(), split(), replace(), find(), format()等,用于处理字符串数据。 ```python s = "hello, world" print(s.upper()) # 输出 'HELLO, WORLD' print(s.split(",")) # 输出 ['hello', ' world'] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值