Index python string sections and common method of operation, for circulating

--- --- restore content begins

First, the index of the string and slice

1. Index

  s = 'ASDFGHJKL'

  Ordered sequence, index --index: from 0

  s1 = s [0], remove the individual elements: A; s1 is independent of a string of new and original string

2. Slice

  # Want to take ASDF:

  s2 = s [0: 4]    Law: care regardless Ding

  # Want to take L:

  s3 = p [-1]    

  # Want to take all

  s4 = s[:] or s[0:]

  s5 = s [0: -1], the last fail to

3. Take jumping, add step

  s = 'ABCDLSESRF'

  s6 = s [0: 5: 2] Take: ACL

4. Take the inverse

  s7 = s [4: 0: -1] Take: LDCB

  s8 = s [3 :: - 1] take: DCBA

  s9 = s [3 :: - 2] taken: DB

  s10 = s [-1 :: - 1] or s = [:: - 1] negated

 

Second, conventional methods of operation string

  s = 'alexWUsir'

1. capitalized capitalize ()

  s1 = s.capitalize()

2. All capitalized Upper () , all lowercase lower ()

  s2 = s.upper()

  s21 = s.lower () eg: Demand - codes insensitive, how to determine whether the correct input

 

3. Flip case swapcase ()

  s3 = s.swapcase ()

4. spaced word capitalized (with spaces or special characters, numbers can be separated) title ()

  s = alex egon wusir

  s4 = s.title()

 

 

 The center, add the total width of the filling; Center ()

  s = 'alexWUsir'

  s5 = s.center(20,' ')

 6.8 a separated, not full complement, expandtabs ()

  s = 'alex \ survivors'

  s6 = s.expandtabs()

 

# Public Methods

len () --- seek length

 

7. Analyzing string beginning with what startwith (), the mirror method endwith (), returns True / False

  s = 'alexWUsir'

  s7 = s.startwith('alex')    #True

  s71 = s.startwith('e',2,5)

8. lookup indexed by elements not found returns -1 Find ()

  s = 'alexWUsir'

  s8 = s.find ( 'W') returns the index: a plurality of elements 4, with the first element index prevail

  s8 = s.index('A')

9. Find the index through the elements, can not find the error  index ()

10. Delete the default string trailing spaces , Strip ()

  Not just a space, you can modify the default values. % * 

  Variants: rstrip (), lstrip ()  to delete the left to the right elements

11. statistics, count ()

  s = 'alexWUsir'

  s9 = s.count('al')   # 1 

12. The split, split, Split () into a list of    strings turn list

  s = 'alex WUsir taibai'

  l = s.split () # default list is divided into a space

  s = 'alex:WUsir:taibai'

  l = s.split(':') 

  Delimiter will disappear (spaces: other delimiter of your choice)

13. formatted output, the format () three kinds of play

  s = 'My name is {}, {year}, {} hobby, to tell you my name is {}'. format ( 'john', '18', 'ball', 'john') # correspondence

  s = 'My name is {0}, {1} this year, hobbies, {2}, to tell you my name is {0}'. format ( 'john', '18', 'ball') # corresponding index

  s = 'My name is {name}, this {age}, hobby {hobby}, to tell you my name is {name}'. format (age = 18, name = 'john', hobby = 'ball') # key-value pairs

14. Alternatively --replace () old new count

  s = "view neighborhood See neighborhood milk remember to play."

  s1 = s.replace ( "Block", "Wang") replace all default

15.is series

  = name 'taibai123' 
  Print (name.isalnum ()) # string of letters or numbers 
  print (name.isalpha ()) # string contains only the alphabet 
  string print (name.isdecimal ()) # of only decimal composition 

three, for loop
limit cycle, iterables
S = 'sdasfsfr'
for I in S:
  Print (I)

 

 

 

 

Guess you like

Origin www.cnblogs.com/RevelationTruth/p/11461750.html