String Function

String Function

Universal point

  • str:. input str in pycharm will be displayed in many functions.

upper () / lower () letters case (result is str)

  • Variable names .upper () function: a string of variable capital letters changed, but does not change the original variable content.

    eg:

    value = "alex sb"
    new_value = value.upper() #这里注意不能只写value.upper(),因为这个功能只能进行赋值,不能改变原有变量中字母的大小写。故将其变大之后的字符串赋值给一个新的变量。
    print(value,new_value)
  • Variable names .lower () function: variable string of letters to lower case

  • eg:

isdigit () determines whether str can be converted into int (the result is bool)

  • Variable names .isdigit () If the conversion, output True; if not converted, will output False

  • eg:

Strip (str) removing the head and tail (the result is str)

  • strip () If you do not write the characters in parentheses, the default is to remove blank "space & tab & enter", but does not remove the middle of the character blank
  • You can specify the head and tail ends of the removed content
  • tab: Tab ---> pycharm by \ n denotes
  • enter: newline ---> pycharm by \ t for
  • space

  • Variable name .strip () to remove the character string left spaces on both sides

  • Variable names .rstrip () to remove the spaces to the right of the string

  • Variable names .lstrip () to remove the string of spaces to the left of

  • eg:

  • eg:

    name = "\n\t   alex    a \n"
    new = name.strip()
    print(new)    --->    输出结果是"alex   \n"。alex前以及a后的空白全部被消除,但alex与a之间的空白没有被消除,因为内部空白被消除就改变了变量的内容。
    
    指定去除头尾两端的内容:
    name = "al\nex   \t a"
    new = name.strip("a") --->    输出结果是"l\nex   \t"       
    因为括号内输入了"a",所以space,tab,enter都没有被消除,只有a被消除了
  • Scenario:

    # 账号:alex 密码:alex123
    # alex   alex123
    # user = input("账号:").strip()
    # pwd = input("密码:").strip()
    # if user == "alex" and pwd == "alex123":
    #     print("ok")
    # else:
    #     print("gun")

replace (str, str, int) replaced (the result is str)

  • Variable name .replace ( 'xx', 'yy', digital) Function: variable specified Replace xx yy. In brackets are the number of the replacement, the order from left to right. If you do not write the number, the default Replace All

  • eg:

split (str, int) cut (returns a list)

  • .Split variable name ( 'character', digital) Function: a character string inside the parentheses the full cut portion of the character variables, the number in brackets are cut, from left to right. If not write characters, the default cut blank (i.e., space, enter, tab); can not digital (i.e., not limited to the number of cutting)

  • Difference variable name .rsplit ( 'character', numbers) and split in that: After the addition of the numbers defining the number of cutting, the cutting sequence of the opposite (right to left)

  • eg:

  • a = "alex:alex123"
    lst = a.split(":")  # 默认按照空格及换行符,制表符进行分割
    print(lst[0])
    print(lst[1])
    # 这里同字符串的索引,索引是指各个字符在一个字符串里的位置,这里为列表中各个字符串在列表里的位置
    ['alex', 'alex123'] 列表
  • name = "alex\tmeet"
    print(name.split("e"))
    --->  输出结果是['al', 'x\tm', '', 't']

name [int] :( subscript index)

  • name [xxx] xxx represents the first of several

  • eg:

    name = "meet"
         #0123(索引值|下标志)   从左向右
         #-4-3-2-1        从右向左:这对于优先输出末尾的字符串比较方便.
    print(namep[0])  ---m
    print(namep[-4])  ---m
    
    name = "meet_alex_wusir"
    a = name[5] #a
    b = name[6] #l
    c = name[7] #e
    d = name[8] #x
    print(a+b+c+d)    --->    alex

name [-1: 3: -1] + slicing step

  • name [Start position: ending position +1] is not written until the end position of the end of the default; default writing start positions not scratch

  • name = "meet_alex_wusir"
    a = name[5] #a
    b = name[6] #l
    c = name[7] #e
    d = name[8] #x
    print(a+b+c+d)        or      print(name[5:9])
    注意:[起始位置:终止位置+1]  
    顾头不顾腚,所以腚的位数要+1
    如果不写终止位置,即[起始位置:],则会从起始位置输出至完.
    
    如果要输出wusir:       print(name[-5:]) 这里如果写-5:-1则最后一位不会输出,然而他的最后一位还不能+1,因为0是正数的第一位,-1是反数的第一位。所以只能不写,让他输出至完。
    
    如果要输出e_l:     print(name[2:7:2])  步长:写在最后,不写时默认为一。此时为2,意为一次跨两步,即输出2,4,6。
    
    print(name[-1:3:-1])  --->    输出结果:risuw_xela_    顾头不顾腚,所以没有t。当步长为负数时顺序就变成了从右到左。
    
    因此,补偿可以控制查找方向

to sum up:

  • When the index, if the index value exceeds the maximum error
  • Slice, slice over the maximum value without error
  • Indexing and slicing can only give an ordered data type

id(str)

  • String --str

    Ordered: Index

    Immutable: Memory Address

    # name = "meet"
    # print(id(name))
    # name = name + "最帅了"
    # print(id(name)) --->    两个id是不一致的
    
    # name = "meet"
    # name1 = "meet"
    # print(id(name))     两个id是一致的
    # print(id(name1))  # 小数据池 -- 驻留机制

startswith (str) / endswith () at the beginning / end (support sections)

Statistics count (str / int) (to accept the slice)

  • name = "meet_alex"
  • print(name.count("e")) --> 3
  • print(name.count("ee")) --> 1

series is

  • print (name.isalnum ()) # variable determines whether numbers, letters, Chinese characters
  • print (name.isalpha ()) # variable determines whether the letters, Chinese characters
  • print (name.isdigit ()) # determine whether a variable is Arabic numerals ⑤: This is True, a BUG
  • print (name.isdecimal ()) # decimal judgment is not, returns a Boolean value

len (str) string length

  • 方法:
    # a = 123132
    # print(len(a)) #-- str,dict,list...公共方法 (获取长度)
    
    """
    a
    l
    e
    x
    """
    # count = 0
    # while count < len(name):
    #     print(name[count])  --->a\n,l\n,e\n,x\n
    #     count += 1

Guess you like

Origin www.cnblogs.com/Guoxing-Z/p/11494976.html