Python - 字符串操作

易涨易退山溪水,易反易覆小人心

三引号字符串

如果字符串跨越一行以上,可以使用三引号。

string = """
    昔时贤文,诲汝谆谆。
    集韵增广,多见多闻。
    观今宜鉴古,无古不成今。
    """

索引

和列表,元组一样,字符串也是可迭代的,可以i使用索引取查找其中的元素,第一个元素对应的索引为0。最后一个元素索引为-1

字符串不可变

字符串和元组一样不可变,如果要修改字符串,可使用当前变量再重新赋值,将原先的值覆盖即可。

字符串加法

多个字符串拼接可使用 +

string = "i" + "am" + "zhutou"

字符串乘法

实现N个自身拼接。

string = "hello"*3

改变大小写

def main():
    str1 = "my name is zhutou".upper() #大写
    str2 = "YOU CAN CALL ME ZHUTOU".lower()#小写
    print(str1)
    print(str2)

if __name__=='__main__':
    main()
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
MY NAME IS ZHUTOU
you can call me zhutou

格式化

使用format 方法创建新字符串,将{}替换为传入的字符串。

name = input('please input your name: ')
print("Hello! {}".format(name))
print("My name is {}".format("zhutou"))
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
please input your name: lin
Hello! lin
My name is zhutou

分割

使用 split 方法可以将字符串分割为两个或多个字符串。

string = "I jumped over the puddle. It was 12 feet!".split(".")
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
['I jumped over the puddle', ' It was 12 feet!']

连接

使用 join 方法可以在字符串的每个字符间添加新字符。

string = "abc"
result = "+".join(string)
print(result)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
a+b+c

将列表转换为字符串

lists = ["i" , "am" , "zhutou"]
result = " ".join(lists)
print(result)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
i am zhutou

去除空格

使用 strip 方法去除字符串开头和结尾的空白字符,中间的空白字符不会被去除。

print(" hello,zhutou ".strip())
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
hello,zhutou

查找索引

使用 index 方法来获得字符串中某一个字符第一次出现的索引。

addr = "zhutou".index("u")
print(addr)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
2

in/not in关键字

检查某个字符串是否在另一个字符串中,返回 True/False。

bool1 = "Cat" in "Cat in the hat."
bool2 = "Bat" not in "Cat in the hat."
print(bool1)
print(bool2)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
True
True

反斜杠转义

如下,在双引号里面还出现双引号,单引号里面出现单引号,则需要\转义。

string1 = "i am \"zhutou\""
string2 = "i am 'zhutou'"
string3 = 'i am "zhutou"'
string4 = 'i am \'zhutou\''
print(string1)
print(string2)
print(string3)
print(string4)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
i am "zhutou"
i am 'zhutou'
i am "zhutou"
i am 'zhutou'

切片

语法: [可迭代对象][起始索引:结束索引]

string = "i am zhutou"
result1 = string[0:3]
result2 = string[:3]
result3 = string[3:]
print(result1)
print(result2)
print(result3)
================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
i a
i a
m zhutou

例题

对字符串"Where now? Who now? When now?"调用一个方法,返回如下述的列表["Where now?", "Who now?", "When now?"]

① 源码

其中正则表达式 [^\?]+\? 中括号表示一个字符集合,^表示不接受该方括号表达式中的字符集合,\?表示匹配问号,单独的问号?匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。所以,[^\?]表示匹配非?的字符(结果:['W', 'h', 'e', 'r', 'e', '', 'n', 'o', 'w', '', 'w', 'h', 'o', '', 'n', 'o', 'w', '', 'w', 'h', 'e', 'n', '', 'n', 'o', 'w']),[^\?]+\?就是表示在前面的匹配基础上再加上一个问号结尾的字符串。

import re
str="Where now? who now? when now?"
res = [x.strip() for x in re.findall('[^\?]+\?',str)]
print(res)

② 结果

================= RESTART: C:/Users/Lin/Desktop/test/test.py =================
['Where now?', 'who now?', 'when now?']
                                                                                                                                                                                           猪头
                                                                                                                                                                                        2020.4.22
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值