zoukankan      html  css  js  c++  java
  • python str 的常用方法

    字符串方法

    Python 有一组可以在字符串上使用的内建方法。

    注释:所有字符串方法都返回新值。它们不会更改原始字符串。

    https://www.w3school.com.cn/python/python_strings.asp

    下面列出几个常用的  其他需要在查询

    len(str)   返回字符串的长度

    z='abacdklfjaslfj'
    print(len(z))#14

    str.strip() 去除字符串首,尾的空格

    E='  hello ,world  '
    print(E.strip())#hello ,world

    str.upper()字符串转换成大写字符串

    F='hello world'
    print(F.upper())#HELLO WORLD

    str.lower()字符串转换成小写字符串

    G='HELLO WORLD'
    print(G.lower())#hello world

    str.replace(str1,str2) 用str2替换掉str中的str1

    H='hello world'
    print(H.replace('hello','happy'))#happy world

    str.split() 把字符串切割成元组;默认已空格切割

    J='hello,world'
    print(J.split(','))#['hello', 'world']

    str1 in str    关键字in  检查 str1是否存在str中

    K='hello world'
    print('hello' in K) #True

    str.format(st1,str2)    format函数会使用传递的参数str1,str2等依次格式化字符串中的{} ;(format() 方法接受不限数量的参数,并放在各自的占位符中:

    #字符串格式str.format()
    L='my name is Bill,i am {}'
    print(L.format('hello world'))#my name is Bill,i am hello world
    #您可以使用索引号 {0} 来确保参数被放在正确的占位符中:
    M='my name {2} age {0} sex {1}'
    print(M.format(18,'','李四'))#my name 李四 age 18 sex 男
  • 相关阅读:
    HTML5基础内容(二)
    JavaScript逻辑运算符
    JavaScript自增运算符和自减运算符
    JavaScript算数运算符和一元运算符
    CSS分页
    HTML和CSS遇到的细节问题
    JavaScript数据类型转换
    JavaScript标识符
    HTML5基础知识汇总(一)
    CSS颜色透明度
  • 原文地址:https://www.cnblogs.com/lvlisn/p/15109197.html
Copyright © 2011-2022 走看看