Python:字符串

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UqgEmFbe-1596362898757)(F:\JianShu_material\Python\图片\字符串\字符串.png)]

1. 字符串的运算符

  • +

    # +
    name = "张三"
    age = 18
    # 张三的年龄为18
    print(name + "的年龄为:" + str(age))
    
    # *
    str1 = "abc"
    # abcabcabcabcabc
    print(str1 * 5)
    
  • in、not in

    # in、not in
    str2 = "steven"
    result1 = "s" in str2
    result2 = "st" in str2
    result3 = "e" not in str2
    result4 = "ev" not in str2
    # True True False False
    print(result1,result2,result3,result4)
    
  • %

    # %
    name = "张三"
    age = 18
    # 张三的年龄为18
    print("%s的年龄为%s" % (name,age))
    
  • r

    # r
    # HelloWorld	!!!
    print("HelloWorld\t!!!")
    # HelloWorld\t!!!
    print(r"HelloWorld\t!!!")
    
  • [ ]、[ : ]

    # []、[:]
    str3 = "HelloWorld"
    print(str3[3]) #l
    print(str3[1:5]) #ello
    print(str3[5:]) #World
    print(str3[-1:]) #d
    print(str3[-4:-1]) #orl
    
  • [ : : ]

    # [::]
    str4 = "abcdefghi"
    print(str4[::-1]) #ihgfedcba
    print(str4[-2:-6:-1]) #hgfe
    print(str4[5:0:-1]) #fedcb
    print(str4[0:5:-1]) #空
    print(str4[1:5:1]) #bcde
    print(str4[::2]) #acegi
    print(str4[::-2]) #igeca
    

2. 字符串方法之大小写

capitalize():将字符串第一个字符转成大写

message = "zhangsan is a beautiful boy"
mes = message.capitalize()
# Zhangsan is a beautiful boy
print(mes)

title():将每个单词的第一个字符转成大写

message = "zhangsan is a beautiful boy"
mes = message.title()
# Zhangsan Is A Beautiful Boy
print(mes)

upper():将每个字符转成大写

message = "zhangsan is a beautiful boy"
mes = message.upper()
# ZHANGSAN IS A BEAUTIFUL BOY
print(mes)

lower():将每个字符转成小写

message = "ZHANGSAN IS A BEAUTIFUL BOY"
mes = message.lower()
# zhangsan is a beautiful boy
print(mes)

3. 字符串方法之查找与替换

find()

检测str是否包含在字符串中,如果指定范围beg和end,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1。

# find("要查找字符串",start,end)
str1 = "hello world wo ai bei jing tian an men"
s1 = str1.find("wo")
s2 = str1.find("ai",3,17)
print(s1) #6
print(s2) #15

rfind()

find()是从左往右查找一次,rfind()是从右往左查找一次。

# rfind()
str1 = "hello world wo ai bei jing tian an men"
s1 = str1.rfind("an")
s2 = str1.rfind("ti",3,-1)
print(s1) #32
print(s2) #27

index()

跟find()方法一样,只不过如果str不在字符串中会报一个异常。不常使用这种查找方式。

replace()

把将字符串中的str1替换成str2,如果max指定,则替换不超过max次。

# replace
str1 = "hello world wo ai bei jing tian an men"
s1 = str1.replace(" ","--")
s2 = str1.replace(" ","--",2)
print(s1) #hello--world--wo--ai--bei--jing--tian--an--men
print(s2) #hello--world--wo ai bei jing tian an men

4. 字符串方法之判断开头结尾

startswith():判断字符串的开头是否为指定字符串

str = "HelloWorld"
result = str.startswith("h")
print(result) #False

endswith():判断字符串的结尾是否为指定字符串

filename = "笔记.doc"
result = filename.endswith("txt")
print(result) #False

5. 字符串方法之判断是否是数字或字母

isalpha():判断字符串是否全是字母

mes = "asedfg1"
result = mes.isalpha()
print(result) #False

isdigit():判断字符串是否全是数字

mes = "56685"
result = mes.isdigit()
print(result) #True

6. 字符串方法之合并拆分

join():用指定字符串合并原有字符串或列表

str = "abc"
new_str = "-".join(str)
print(new_str) #a-b-c

list = ['a','v','o']
new_str = "".join(list)
print(new_str) #avo

split():分割字符串,将分割后的字符串返回在列表中

str = "hello world hello kitty"
result1 = str.split(" ")
result2 = str.split(" ",2) #按照分隔符分割2次
print(result1) #['hello', 'world', 'hello', 'kitty']
print(result2) #['hello', 'world', 'hello kitty']

count():返回指定字符串出现的次数

str = "hello world hello kitty"
result = str.count(" ")
print(result) #3

7. 字符串方法之编码和解码

encode()与decode():编码与解码

mes = "大家好,我是张三"
result = mes.encode("utf-8")
# b'\xe5\xa4\xa7\xe5\xae\xb6\xe5\xa5\xbd\xef\xbc\x8c\xe6\x88\x91\xe6\x98\xaf\xe5\xbc\xa0\xe4\xb8\x89'
print(result)
m = result.decode("utf-8")
# 大家好,我是张三
print(m)

8. 字符串方法之去除空格

strip():去除字符串前后两端空格

lstrip():去除字符串前端空格

strip():去除字符串后端空格

str = "    hello    "
str1 = str.lstrip()
str2 = str.rstrip()
str3 = str.strip()

print(str) #    hello 
print(str1 + "8") #hello    8
print(str2 + "8") #    hello8
print(str3 + "8") #hello8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值