zoukankan      html  css  js  c++  java
  • (01)-Python3之--字符串操作

    1.字符串切片取值

    字符串的取值通过索引来读取,从0开始。

    取区间值如下:字符串变量名[起始索引:结束索引]。包含起始,但不包含结束。
    例如:

    str_my = "hello,python!我来了!"
    print(str_my[0:4])  # 取0,1,2,3位
    print(str_my[0:5])  # 取0,1,2,3,4位
    # 从第6个开始,一直取到最后。
    print(str_my[5:])
    # 从头开始,取到索引下标为7
    print(str_my[:8])
    print(str_my[0:8])
    
    结果:
    hell
    hello
    ,python!我来了!
    hello,py
    hello,py

    2.字符串长度

    获取字符串长度一般用len函数

    str_my = "hello,python!我来了!"
    print(len(str_my))
    
    结果:
    23

    3.查找子字符串

    语法:字符串变量名.find(子字符串)

    如果找到了,返回的是起始索引。如果没有找到了,返回的是-1。

    语法:字符串变量名.index(子字符串)
    与find类似但找不到子串时会引发异常

    例如:

    str_my = "hello,python!我来了!"
    # 查找python
    print(str_my.find("python"))
    # 查找ph
    print(str_my.find("ph"))  
    # 查找!
    print(str_my.find("!"))
    
    结果:
    6
    -1
    12

    rfind() 返回字符串最后一次出现的位置,如果没有匹配项则返回-1。

    语法:str.rfind(str, beg=0 end=len(string))

    • str -- 查找的字符串
    • beg -- 开始查找的位置,默认为0
    • end -- 结束查找位置,默认为字符串的长度。
    filename = r'E:UsersWeb_test	est1.py'
    pos = filename.rfind('.')
    print(pos)
    suffix = filename[pos:]
    print(suffix)

    结果:
    .py

    4.替换操作

    语法:字符串变量.replace(old,new)

    例如:

    str_my = "hello,python!我来了!"
    # 用$来替换!
    new_str = str_my.replace("!","$")
    print(new_str)
    # 用$来替换!且只替换一个。
    new_str = str_my.replace("!","$",1)
    print(new_str)
    
    结果:
    hello,python$我来了$
    hello,python$我来了!

    5.字符串大小写转换

    小写字母转换成大写字母:upper()

    大写字母转换成小写字母:lower()

    把第一个字母转化为大写字母,其余小写:capitalize()

    把每个单词的第一个字母转化为大写,其余小写:title()

    例如:

    # 小写字母转换成大写字母:upper()
    str_my = "www.baidu.com"
    print(str_my.upper())
    
    # 大写字母转换成小写字母:lower()
    str_my = "WWW.BAIDU.COM"
    print(str_my.lower())
    
    # 把第一个字母转化为大写字母,其余小写:capitalize()
    str_my = "www.baidu.com"
    print(str_my.capitalize())
    
    # 把每个单词的第一个字母转化为大写,其余小写:title()
    str_my = "www.baidu.com"
    print(str_my.title())
    
    结果:
    WWW.BAIDU.COM
    www.baidu.com
    Www.baidu.com
    Www.Baidu.Com

    6.删除字符串

    删除字符串左右(头尾)两边的空格或者指定的字符串。

    语法:字符串变量名.strip([指定的字符串])

    例如:

    例一:
    str_a = " 11python31,class3 " # 删除头尾的空格 new_str = str_a.strip() print(new_str) # 删除头尾指定的11 new_str2 = new_str.strip("1") print(new_str2) # 删除左边空字符 new_str3 = str_a.lstrip() print(new_str3) #删除右边空字符 new_str3 = str_a.rstrip() print(new_str3) 结果: 11python31,class3 python31,class3 11python31,class3 11python31,class3
    例二:
    str_a = '  -----abc123++++       '
    # 删除两边 - + 和空字符
    print(str_a.strip().strip('-+'))
    
    结果:
    abc123
    例三:
    str_a = 'abc:123'
    # 删除单个固定位置字符使用切片+拼接
    # 字符串拼接方式去除冒号
    new_str_a = str_a[:3] + str_a[4:]
    print(new_str_a)
    
    结果:
    abc123

    7.字符串截断

    字符串截断是按照指定的分隔符进行字符串的截断。

    语法:字符串变量名.split(分隔符)

    例如:

    str_b = "大家好,我是python。今天的天气真好,上课很开心。一会儿就中场休息。"
    # 使用.split(分隔符)分割字符串。
    result = str_b.split("")
    print(result)
    # 指定分隔的次数
    result_2 = str_b.split("",1)
    print(result_2)
    
    结果:
    ['大家好,我是python', '今天的天气真好,上课很开心', '一会儿就中场休息', '']
    ['大家好,我是python', '今天的天气真好,上课很开心。一会儿就中场休息。']

    8.字符串拼接

    用连接符,将列表当中字符串拼成一个字符串。

    要求:列表当中每一个值都要是字符串。

    语法:连接符.join(列表)

    例如:

    list_a = ['大家好,我是Python', '今天的天气真好,上课很开心', '一会儿就中场休息', '']
    list_b = " $我是连接符$ ".join(list_a)
    print(list_b)
    
    结果:
    大家好,我是Python $我是连接符$ 今天的天气真好,上课很开心 $我是连接符$ 一会儿就中场休息 $我是连接符$ 

    9.检查字符串是否以指定的字符串开头和结尾

    语法:字符串变量名.startswith('字符串开头字符')   # False或True
    语法:字符串变量名.endswith('字符串结尾字符')   # False或True
    例如:
    list_a = '大家好,我是Python,今天的天气真好,上课很开心,一会儿就中场休息'
    list_b = list_a.startswith('大家好')
    list_c = list_a.endswith('我是Python')
    print(list_b)
    print(list_c)
    
    结果:
    True
    False

    10.宽度居中并在两侧填充指定的字符

    语法:字符串变量名.center(50,'*')  # 将字符串以指定的宽度居中并在两侧填充指定的字符

    语法:字符串变量名.rjust(50,' ')  # 将字符串以指定的宽度靠右放置左侧填充指定的字符

    list_a = '大家好,我是Python,今天的天气真好,上课很开心,一会儿就中场休息'
    # 将字符串以指定的宽度居中并在两侧填充指定的字符
    list_b = list_a.center(50,'*')
    # 将字符串以指定的宽度靠右放置左侧填充指定的字符
    list_c = list_a.rjust(50,' ')
    print(list_b)
    print(list_c)
    
    结果:
    *******大家好,我是Python,今天的天气真好,上课很开心,一会儿就中场休息********
                   大家好,我是Python,今天的天气真好,上课很开心,一会儿就中场休息

    11.检查字符串是否由数字、字母构成

    str_a = '123456'
    # 检查字符串是否由数字构成
    print(str_a.isdigit())
    # 检查字符串是否以字母构成
    print(str_a.isalpha())
    # 检查字符串是否以数字和字母构成
    print(str_a.isalnum())
    
    结果:
    True
    False
    True

    12.格式化字符串

    方式一:%使用

    %s 字符串

    %d 数字

    %f 浮点数

    例如:

    str_a = "我今年的目标是:薪资上涨%d,达到月薪%d。" % (5000,10000)
    print(str_a)
    
    结果:
    我今年的目标是:薪资上涨5000,达到月薪10000。

    方式二:format

    占位符{}

    例如:

    str_a = "我今年的目标是:薪资上涨 {} ,达到月薪 {} ".format(5000,10000)
    print(str_a)
    # 占位符{0}{1}
    str_b = "我今年的目标是:薪资上涨 {1} ,达到月薪 {0} ".format(10000,5000)
    print(str_b)
    # 占位符{0}{0}
    str_c = "我今年 {0} 岁,我希望我每年都是 {0} 岁。".format(18)
    print(str_c)
    
    结果:
    我今年的目标是:薪资上涨 5000 ,达到月薪 10000 
    我今年的目标是:薪资上涨 5000 ,达到月薪 10000 
    我今年 18 岁,我希望我每年都是 18 岁。
  • 相关阅读:
    jsonp 的 post
    js replace常用用法
    zindex
    x秒前
    手写jsonp
    webview 冒泡慢?
    人民币大写转阿拉伯数字
    checked
    Deadlock Troubleshooting Trace 1222
    [转]基于LUCENE实现自己的推荐引擎
  • 原文地址:https://www.cnblogs.com/renshengruxi/p/11927199.html
Copyright © 2011-2022 走看看