zoukankan      html  css  js  c++  java
  • python字符串及字符串操作

    字符串介绍


    1、字符串在内存中的存储;

    2、字符串相加;

    3、字符串的格式化;

    In [1]: a = 100 
    
    In [2]: a
    Out[2]: 100   #100<255,在堆内存下占用了一个字节,一个字节8位,可以存放的数值最大为255。
    
    In [3]: b = "100"
    
    In [4]: b
    Out[4]: '100' #对应ASCII码存放,一个字节存放任何的一个字符,因此字符串100对应3个字符即占用3个字节。字符串占用空间大。
    
    In [5]: type(a)
    Out[5]: int
    
    In [6]: type(b)
    Out[6]: str
    In [4]: b =  '100'
    In [7]: c = "200"
    In [8]: b + c
    Out[8]: '100200'
    In [9]: d = "b+c=%s" %(b+c)
    
    In [10]: d
    Out[10]: 'b+c=100200'
    In [11]: e="abcefg%sgfgfgfg"%b
    
    In [12]: e
    Out[12]: 'abcefg100gfgfgfg'

    下标索引:就是编号,通过这个编号能找到相应的存储空间。

    字符串中下标的使用:字符串实际上就是字符的数组,所以也支持下标索引

    In [27]: name
    Out[27]: 'abcdefghijk'
    
    In [28]: name[0]
    Out[28]: 'a'
    
    In [29]: name[-1]
    Out[29]: 'k'
    
    In [30]: len(name)
    Out[30]: 11
    
    In [32]: name[len(name)-1]
    Out[32]: 'k'
    
    In [33]: name[-10]
    Out[33]: 'b'
    
    In [34]: name[10]
    Out[34]: 'k'

    切片:是指对操作对象截取其中一部分的操作,字符串、列表、元组都支持切片操作

    切片的语法:【起始:结束:步长】

    步长是下标变化的规律,默认为1

    注意:选取的区间属于左闭右开型(包头不包尾)

    In [1]: name="abdfsdfsdf"
    
    In [2]: name[1:4]
    Out[2]: 'bdf'
    
    In [3]: name[:]
    Out[3]: 'abdfsdfsdf'
    
    In [4]: name[::2]
    Out[4]: 'adsfd'
    
    In [5]: name[0:]
    Out[5]: 'abdfsdfsdf'
    
    In [6]: name[:-1]
    Out[6]: 'abdfsdfsd'

    将输入的字符串倒序输出

      1 s=input("请输入一个字符串:")
      2 i=len(s)
      3 while i > 0 :
      4     i -= 1
      5     print("%s"%(s[i]),end="")
      6 print("")
    [root@localhost python]# python3 20.py 
    请输入一个字符串:love 
    evol
    [root@localhost python]#

    通过切片实现字符串倒序输出

    In [1]: name="asdsadsds"
    
    In [2]: name
    Out[2]: 'asdsadsds'
    
    In [3]: name[-1::-1]
    Out[3]: 'sdsdasdsa'

    字符串操作:函数调用的操作

    In [9]: name="love"
    
    In [10]: name. //按Tab键显示字符串的方法
       name.capitalize   name.encode       name.format       name.isalpha      name.islower      name.istitle      name.lower         
       name.casefold     name.endswith     name.format_map   name.isdecimal    name.isnumeric    name.isupper      name.lstrip        
       name.center       name.expandtabs   name.index        name.isdigit      name.isprintable  name.join         name.maketrans    >
       name.count        name.find         name.isalnum      name.isidentifier name.isspace      name.ljust        name.partition    

     查找方法:find()和index()

    In [10]: my_str="hello world zyj sl everyone in the world"
    
    In [11]: my_str.find("zyj")
    Out[11]: 12  #目标字符串的第一个字符所在的下标位
    
    In [12]: my_str.find("lw")
    Out[12]: -1 #找不到时返回-1,当返回小于0时,即可判断没有找到。
    
    In [13]: my_str.find("world")
    Out[13]: 6 #默认从左边开始查找,并输出第一个找到的位置
    
    In [14]: my_str.rfind("world")
    Out[14]: 35 #.rfind方法从右边开始查找
    
    In [15]: my_str.index("world")
    Out[15]: 6 #index()默认从左边查找
    
    In [17]: my_str.rindex("world")
    Out[17]: 35 #rindex()从右边查找
    
    In [16]: my_str.index("lw")
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-16-a951699b322b> in <module>()
    ----> 1 my_str.index("lw")
    
    ValueError: substring not found #查找不到时报错,与find()的区别

    计数及替换方法:count()和replace()

    In [18]: my_str.count("world")#计算目标元素出现的次数。
    Out[18]: 2
    
    In [19]: my_str.count("lw")
    Out[19]: 0
    
    In [20]: my_str.count("zyj")
    Out[20]: 1
    
    In [21]: my_str.replace("hello","Hi")
    Out[21]: 'Hi world zyj sl everyone in the world'
    
    In [22]: 
    
    In [22]: my_str
    Out[22]: 'hello world zyj sl everyone in the world' #原字符串没有改变
    
    In [23]: my_str.replace("world","city")#默认替换所有查找到的目标元素
    Out[23]: 'hello city zyj sl everyone in the city'
    
    In [24]: my_str.replace("world","city",1) #将查找到的第一个替换,指定count,则替换不超过count次。
    Out[24]: 'hello city zyj sl everyone in the world'

    分隔:split(),以str为分隔符切片mystr,可以指定最多分割成多少个段。

    In [25]: my_str
    Out[25]: 'hello world zyj sl everyone in the world'
    
    In [26]: my_str.split(" ") #以空格作为分隔符
    Out[26]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world']
    
    In [29]: my_str.split() #默认以空格作为分隔符,返回的是列表
    Out[29]: ['hello', 'world', 'zyj', 'sl', 'everyone', 'in', 'the', 'world']
    
    In [27]: my_str.split(" ",2) #指定最多包含两个分隔符
    Out[27]: ['hello', 'world', 'zyj sl everyone in the world']
    
    In [28]: my_str.split("zyj")
    Out[28]: ['hello world ', ' sl everyone in the world'] #结果中不显示“zyj”,把它当作分隔符了。
    
    
    In [30]: my_str.partition("zyj")
    Out[30]: ('hello world ', 'zyj', ' sl everyone in the world') #将本身也作为一个元素,与split()的区别,包含隔开符。
    分隔:partition()注意与split()的区别。
    In [25]: my_str
    Out[25]: 'hello world zyj sl everyone in the world'
    
    
    In [36]: my_str.partition() #不支持这种写法,必须指定分隔标志。
    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-36-c027286912d6> in <module>()
    ----> 1 my_str.partition()
    
    TypeError: partition() takes exactly one argument (0 given)
    
    In [37]: my_str.partition(" ") #以空格作为分隔符,遇到的第一个空格作为分隔符,自身也是分隔元素
    Out[37]: ('hello', ' ', 'world zyj sl everyone in the world')
    
    In [38]: my_str.partition(' ',3) #不支持设置最大分隔数
    ---------------------------------------------------------------------------
    TypeError Traceback (most recent call last)
    <ipython-input-38-86d354aca67f> in <module>()
    ----> 1 my_str.partition(' ',3)
    
    TypeError: partition() takes exactly one argument (2 given)

    capitalize():把字符串的第一个字符大写。

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

    In [41]: my_str.capitalize()
    Out[41]: 'Hello world zyj sl everyone in the world'
    
    In [42]: my_str.title()
    Out[42]: 'Hello World Zyj Sl Everyone In The World'

    startswith():检查字符串以什么开头,返回布尔值:True或False

    endswith():检查字符串以什么开头,返回布尔值:True或False

    In [43]: my_str.startswith("hello")
    Out[43]: True
    
    In [44]: my_str.startswith("Hello")
    Out[44]: False
    
    In [45]: my_str.endswith("wor")
    Out[45]: False
    
    In [46]: my_str.endswith("world")
    Out[46]: True
    
    #使用场景
    In [50]: file_name="XXX.txt"
    
    In [52]: if file_name.endswith(".txt"):
        ...:     print("文本文件")
        ...:     
    文本文件
    In [53]:

    lower():将字符串中的所有大写字符变为小写;

    upper():将字符串中的所有小写字母变为大写;

    In [57]: my_str
    Out[57]: 'Hello WEWER dsfsdf dfsdf'
    
    In [58]: my_str.upper()
    Out[58]: 'HELLO WEWER DSFSDF DFSDF'
    
    In [59]: my_str.lower()
    Out[59]: 'hello wewer dsfsdf dfsdf'
    
    #应用场景,验证码不区分大小写,用户输入的进行转换。
    In [61]: str="ABC" #目标字符串
    
    In [62]: user_str="Abc" #用户输入字符串
    
    In [63]: user_str.upper() #对用户输入的进行转换后比较
    Out[63]: 'ABC'

     排列对齐操作:ljust() rjust() center()

    In [66]: name
    Out[66]: 'ddsfsdfsdfdfdsf'
    
    In [67]: name.ljust(50) #50代表长度,返回一个使用空格填充至长度的新字符串
    Out[67]: 'ddsfsdfsdfdfdsf                                   '
    
    In [68]: 
    
    In [68]: name.rjust(50)
    Out[68]: '                                   ddsfsdfsdfdfdsf'
    
    In [69]: name.center(50)
    Out[69]: '                 ddsfsdfsdfdfdsf                  '
    
    In [70]: 

    删除空白字符:lstrip()   rstrip()  strip()

    In [70]: name=" abc  "
    
    In [71]: name.lstrip() #删除左边的空白符
    Out[71]: 'abc  '
    
    In [72]: name.rstrip() #删除右边的空白符
    Out[72]: ' abc'
    
    In [73]: name.strip() #删除左右两侧空白符 strip()=trim() java中使用trim()
    Out[73]: 'abc' In [74]:

    换行符隔开:splitlines()

    In [76]: lines="anbc
    dfsdfdf
    dfsdfdf
    dfdf"
    
    In [77]: lines.splitlines()
    Out[77]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf']
    
    In [78]: lines.split("
    ")
    Out[78]: ['anbc', 'dfsdfdf', 'dfsdfdf', 'dfdf']
    
    In [79]:

     字符串中只包含数字、字母、数字或字母、空格的操作,返回布尔值 isalnum() isalpha() isdigit() isspace()

    In [1]: test="122323dsfdfsdfsdf"
    
    In [2]: test.isalnum()
    Out[2]: True
    
    In [3]: test.isalpha()
    Out[3]: False
    
    In [4]: test.isdigit()
    Out[4]: False
    
    In [5]: test.isspace()
    Out[5]: False
    
    In [6]: 
    
    In [6]: test1="  "
    
    In [7]: test1.isspace()
    Out[7]: True

    mystr.join(list):mystr中每个字符后面插入list的每个元素后面,构造出一个新的字符串

    应用:将列表转换为字符串

    In [8]: names=["100","200","300"]
    
    In [9]: names
    Out[9]: ['100', '200', '300']
    
    In [10]: "".join(names)
    Out[10]: '100200300'
    
    In [11]: "-".join(names)
    Out[11]: '100-200-300'

     查看方法的帮助文档:


      In [16]: test="122323dsfdfsdfsdf"

    In [14]: help(test.find)
    
    find(...) method of builtins.str instance
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.

    (面试题)给定一个字符串,返回使用空格或者 分割后的倒数第二个字串。

    In [1]: name="hel eewrje	wrjwer	werwer ew
    rewr"
    
    In [6]: help(name.split)
    
    In [7]: name.split() #会将字符串中的空格以及特殊意义的符号作为分隔符。
    Out[7]: ['hel', 'eewrje', 'wrjwer', 'werwer', 'ew', 'rewr']
    
    In [8]: name.split()[-2] #返回的是列表。列表支持下标操作。
    Out[8]: 'ew'
  • 相关阅读:
    ASP.NET Ajax基础-1
    项目管理必读之书-》人月神话
    Discuz2.5菜鸟解析-1
    Jquery初学者指南-1
    敏捷日记
    精品图书大推荐2
    Jquery初学者指南-2
    纯javaScript脚本来实现Ajax功能例子一
    周五面试笑话一则
    JavaScript基础-4
  • 原文地址:https://www.cnblogs.com/zhaoyujiao/p/8999290.html
Copyright © 2011-2022 走看看