zoukankan      html  css  js  c++  java
  • python 字符串常用操作方法

    python 字符串常用操作

     

    字符串常用方法

    capitalize()

    String.capitalize() 将字符串首字母变为大写

    name = 'xiaoming'
    
    new_name = name.capitalize()
    
    print(new_name) 

    运行结果:
    Xiaoming

    count()

    String.count() 统计字符出现的次数

    name = 'xiaoming'
    
    name_num = name.count('i')
    
    print(name_num)  # 2

    center()

    String.center()

    #打印输出字符,让字符串放在中间
    name = 'Libai'
    print(name.center(50,'*'))

    输出结果如下:

    **********************Libai***********************
    

    endswith()

    String.endswith() 判断是否以指定的字符串结尾

    name = 'Libai'
    
    new_val = name.endswith('bai')
    print(new_val)

    结果为:
    True

    find()

    String.find() 查找字符串在原字符串中的位置,返回所在索引值

    name = 'this is test plaintext'
    
    print(name.find('this'))
    print(name.find('is'))

    在find()方法中,同样可以使用切片。

    name = 'this is test plaintext'
    
    test_val = name[name.find('test'):12]
    
    print(test_val)  #test

    字符串的切片用法与列表的使用方式一致。

    format()

    String.format() 输出指定的内容

    user_show_name = 'hello,{name},welcome to here,do you like ,{name}'
    
    print(user_show_name.format(name='yanyan'))

    输出效果如下:

    hello,yanyan,welcome to here,do you like ,yanyan
    

    format_map()

    String.format_map() 将字典中的参数传递进字符串中,输出

    hello = "My name is {name},I am {age} years old.I like {hobby}"
    
    # 使用format_map()方法来传递值
    print(hello.format_map({'name':'yanyan','age':19,'hobby':'music travel'}))

    isalnum()

    String.isalnum() 判断字符串中是否全部为数字或者英文

    复制代码
    test_str01 = 'helloIam19yearsold'
    test_str02 = 'hello,I am 19 years old'
    
    
    print(test_str01.isalnum()) # True
    print(test_str02.isalnum()) # False
    复制代码

    isalnum()方法判断字符串中是否全部为数字或者英文,符合就返回True,不符合就返回False,如果里面包含有符号或者空格之类的特殊字符也会返回False。

    isalpha()

    String.isalpha() 判断字符串中是否全部为纯英文字符

    test_str03 = 'hello I love you'
    test_str04 = 'helloILoveYou'
    print(test_str03.isalpha()) # False
    print(test_str04.isalpha()) # True

    isdigit()

    String.isdigit() 判断字符串中是否全部为整数

    # isdigit() 判断是否为整数
    print('123'.isdigit()) # True
    print('hello'.isdigit()) # False

    isidentifier()

    String.isidentifier() 判断是不是一个合法的标识符

    # isidentifier() 判断是不是一个合法的标识符
    print('test'.isidentifier()) # True
    print('12'.isidentifier()) # False
    print('_aa'.isidentifier()) # True

    判断字符串是否全部为大写或者小写

    # islower() 判断字符串是否全部是小写
    print('Hello,world'.islower()) # False
    # isupper() 判断字符串是否全部为大写
    print('Hello,world'.isupper()) # False

    join()

    sep.join(seq) 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串

    # 创建一个列表
    name = ['张学友','刘德华','郭富城','黎明']
    
    print('--'.join(name))

    输出结果如下:

    张学友--刘德华--郭富城--黎明
    

    ljust()

    String.ljust(size,替换符号) 从前向后开始计算,当字符串的长度超过size时,超过部分用替换符号替代

    rjust()

    String.rjust(size,替换符号) 从后向前开始计算,当字符串的长度超过size时,超过部分用替换符号替代

    lower 将字符串大写变成小写

    String.lower()

    # 创建一个字符串
    str = "hello,I am LiBai,I am 23 years old ,I like travel"
    
    # lower 将字符串大写变成小写
    print(str.lower())

    upper 将字符串小写变成大写

    String.upper()

    # 创建一个字符串
    str = "hello,I am LiBai,I am 23 years old ,I like travel"
    
    # 将字符串小写变成大写
    print(str.upper())

    Tip:上面的lower()方法和upper()方法改变字符串后将改变的结果返回,但是原本的字符串并不会改变。

    lstrip 去掉字符串左边的空格或者回车

    String.lstrip()

    print('-----------')
    # 创建一个字符串
    str = "
    hello,I am LiBai,I am 23 years old ,I like travel"
    
    print(str.lstrip())

    输出结果如下:

    -----------
    hello,I am LiBai,I am 23 years old ,I like travel
    

    除了lstrip 还有rstrip和 strip方法。

    replace 替换

    String.replace(old,new,count) 将字符串中的old字符替换为New字符,count为替换的个数

    str = 'hello,world,hello'
    
    print(str.replace('hello','Hello',1))

    输出的效果如下:
    Hello,world,hello

    split

    String.split() 切割

    复制代码
    str = 'hello,world,hello'
    
    # 默认以空格为分割
    print(str.split()) # ['hello,world,hello'] 单词之间没有空格,所以所有的内容为一个元素
    # 以o为分割
    print(str.split('o')) # ['hell', ',w', 'rld,hell', '']
    # 以逗号分割
    print(str.split(',')) # ['hello', 'world', 'hello']
    复制代码

    splitlines() 以换行为分割

    String.splitlines()

    str = 'hello,
    world,
    hello'
    
    print(str.splitlines()) # ['hello,', 'world,', 'hello']
    1 s="alEx Alec"
    2 ret=s.zfill(11)
    3 print(ret)
    4 #输出结果    00alEx Alec
  • 相关阅读:
    UIStoryBoard 中修改控件borderColor
    iOS自定义AlertView 与 ActionSheet 遮罩提示+弹出动画
    iOS开发 UIWebView+JavaScript 交互总结
    【注入攻击】SQL注入(不完整总结)
    [内存溢出]栈溢出基础版
    [Windows驱动开发]之内存管理
    [找工作]程序员面试宝典【笔记】(part 1)
    [Windows安装]安装程序无法创建新的系统分区,也无法定位现有系统分区
    M1卡分析
    [逆向/壳]脱壳方法
  • 原文地址:https://www.cnblogs.com/wangxudong01/p/14286647.html
Copyright © 2011-2022 走看看