A String basic grammar

------------ ------------ restore content begins

1.type () to test the type of a variable. int is a numeric variable, str is a character variable

name = "yao"
Year = 1993
print( name + str(Year))
>>yao1993

2.ord () is a built-in function to return a character,

   chr () is based on integer worth to the corresponding character.

print(ord("a"))
print(chr(97))
>>97
>>a

3. The strings are many ways you can () to view the dir

print(dir(str))
>>

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

4. Case conversion

name.swapcase () # lowercase variable capital, uppercase to lowercase 

name.capitalize () # capitalized 

name.upper ()       # ALL CAPS 

name.lower ()       # all lowercase 

name.title ()         # the first letter in all caps

The retrieval of a position of the character in the string

name.find ( " the n- " )   # locate the "n" where, starting from 0 
name.index ( " the n- " ) # retrieve elements in a variable position, starting from 0

6. The number of times a search of a character string appearing in

print (name.count("n"))

7. Check the string length

print (len (name))

8. power

pow (x, y) = x ** y = y x to the power

9. absolute value

print(bas(-2))
>>2

10. take the remainder

Print (. 5%. 3 )
 >> 2
 Print (divmod (5,3 )) # take quotient and the remainder 
( 1,2)

11. The maximum or minimum

Print (max (, 2, 3)) # maximum 
>> 3

12. integer, floating point, and a plurality

int (x) # x to become an integer, fractional part is discarded 
a float (x) # x to become floating point, increase the fractional part 
Complex (x) # x to become complex, increasing the imaginary part

13. rounding

= 5.453 X
 Print (round (X, 2)) # round (X, D) are reserved several decimal places D

14. A space or line breaks removed

= name " \ nYao \ n- " 
Print (name.lstrip ()) # removed or left line feed space 
print (name.rstrip ()) # remove the right line feed
print (name.strip ()) # remove all newline symbol

15. Analyzing

Print ( ' AG12 ' .isalnum ()) # determines whether or not composed of numbers and letters 
Print ( ' AG ' .isalpha ()) # determines whether only the letter 
Print ( ' 2 ' .isdigit ()) # determines whether only integer 
print ( '1'.isdecimal ()) # decimal determines whether
print (' a-1R'.isidentifier () ) # determines whether a qualified variable name or identifier, i.e., there are special characters not only have integer and the letter
print ( 'My Name'.istitle ()) # determines whether all capital letters

 16. filling

Print (name.center (50, '% ' )) # 50 to a width, with less than '%' is filled, name the intermediate 
print (name.ljust (50, '% ')) # % less than with up behind fill
print (name. rjust (50, '%')) #% with less than fill, fill front

17. Replace

str.maketrans = p ( ' ABC ' , ' 123 ' ) # p-replacement rules for producing, 
Print ( ' adbce ' .translate (p))      # was then replaced with Translate 

Print ( ' ALEXA ' .replace ( ' A ' , ' A ' ,. 1)) # . 1 showing several alternative

 18. A splice, connector

1.split   # return a list of 
    B = ' www.baidu.com ' 
    C = b.split ( ' . ' )
     Print (C)
     >> [ ' WWW ' , ' baidu ' , ' COM ' ]
 2 .join 
    C = [ ' WWW ' , ' baidu ' , ' COM ' ] 
    D = ' * ' .
    join(c)
    print(d)
    >> www * baidu * com

19. The list is sorted, temporary sort, reverse list

= A [7,2,4,3,1 ] 
a.sort () # default from small to large, (reverse = True) descending 

Print (A)

Print (the sorted (A)) # temporary sort
print (a )
>> [1, 2, 3, 4, 7]

  >>[7, 2, 4, 3, 1]

a.reverse () # reverse list

20. The modified list elements

Name = ['xiao','da','max']
Name[0] = 'You'
print(Name)
>>['You', 'da', 'max']

21. Insert and additive elements

# Insert elements 
the Name = [ ' Xiao ' , ' DA ' , ' max ' ] 
Name.insert (0, ' the Apple ' )
 Print (the Name)
 >> [ ' the Apple ' , ' Xiao ' , ' DA ' , ' max ' ]
 # additive element 
Name.append ( ' Blue ' )
 

22. Removing elements

= The Name [ ' xiao ' , ' DA ' , ' max ' ]
 del the Name [0] # according to the index to delete 
Print (the Name) 

Print (Name.pop ()) # can delete the end of the list of elements, and so that you can use to delete elements 
# may be added index POP (. 1) 
Print (the Name)
 >> max
 >> [ ' Xiao ' , ' DA ' ] 

Name.remove ( ' DA ' ) # position is not known, can remove ()

23./n newline, / t tab, / r carriage return, / b backoff

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/huiguizhe/p/11987156.html