码迷,mamicode.com
首页 > 编程语言 > 详细

Python3——字符串操作

时间:2019-12-23 15:14:00      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:art   upper   索引   异常   hello   下标   函数   lse   串操作   

def main():
    str1 = hello,world!
    # 通过len函数计算字符串的长度
    print(len(str1))  # 13
    # 获得字符串首字母大写的拷贝
    print(str1.capitalize())  # Hello, world!
    # 获得字符串变大写后的拷贝
    print(str1.upper())  # HELLO, WORLD!
    # 从字符串中查找子串所在位置 总是返回所找第一个子串的位置
    print(str1.find(or))  # 8
    print(str1.find(shit))  # -1
    # 与find类似但找不到子串时会引发异常
    # print(str1.index(‘or‘))
    # print(str1.index(‘shit‘))
    # 检查字符串是否以指定的字符串开头
    print(str1.startswith(He))  # False
    print(str1.startswith(hel))  # True
    # 检查字符串是否以指定的字符串结尾
    print(str1.endswith(!))  # True
    # 将字符串以指定的宽度居中并在两侧填充指定的字符
    print(str1.center(50, *))
    # 将字符串以指定的宽度靠右放置左侧填充指定的字符
    print(str1.rjust(50,  ))
    str2 = abc123456
    # 从字符串中取出指定位置的字符(下标运算)
    print(str2[2])  # c
    # 字符串切片(从指定的开始索引到指定的结束索引)
    print(str2[2:5])  # c12
    print(str2[2:])  # c123456
    print(str2[2::2])  # c246
    print(str2[::2])  # ac246
    print(str2[::-1])  # 654321cba
    print(str2[-3:-1])  # 45
    # 检查字符串是否由数字构成
    print(str2.isdigit())  # False
    # 检查字符串是否以字母构成
    print(str2.isalpha())  # False
    # 检查字符串是否以数字和字母构成
    print(str2.isalnum())  # True
    str3 =   jackfrued@126.com 
    print(str3)
    # 获得字符串修剪左右两侧空格的拷贝
    print(str3.strip())


if __name__ == __main__:
    main()

Python3——字符串操作

标签:art   upper   索引   异常   hello   下标   函数   lse   串操作   

原文地址:https://www.cnblogs.com/gjh99/p/12083601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!