Common method calls (center, count, find, startwith, endswith, upper, lower, strip, replace, title)

Common method calls:

Common string methods (method):

Method call syntax :

     object . method name (method parameter)

   illustrate:

     A method call is an expression just like a function call.

   Example:

     'abc'.isalpha() # Determine whether 'abc' is all English letters

     123.isalpha() # Wrong, 123 has no isalpha method

For common methods, see:

python_base_docs_html/str_180408102847.html

Example:

center (total length , 'supplementary characters')

S.center(sub,....)

   >>> "ABC".center(10)

  '   ABC    '

    >>> "ABC".center(20, '#')

    '########ABC#########'

 

   >>>S=”hellp world”

count to find the number

Variable name.count('What to look for'[, start][, end])

>>> S.count('ell')

   1

  >>> S.count('o')

   2

   >>> S.count('o', 6) #How many o's are after the sixth digit

   1

find the location

>>>S.find(’wor’)

6

>>>S.find(’ll’)

2

>>> S.find('llabcd') #Return -1 when not found

   -1

 

>>> S = "ABC123.txt"

what startswith starts with

  >>> S.startswith('ABC')

  True

  >>> S.startswith('hello')

  False

what endswith ends with

  >>> S.endswith('.txt')

  True

  >>> S.endswith('.mp3')

  False

Change case upper , lower

>>>S=”hello world”

>>>S.upper()

“HELLO WORLD”

>>>S.lower()

“hello world”

remove blank strip

S=”    \r\n hello world    \t\n”

S.strip() cuts off the start position and end position of whitespace characters, and the focus is reserved, including ' '

S.rstrip() trims spaces on the left

S.lstrip() trims spaces on the right

s = input("Press q to exit")

if s.strip().lower() == "q":

    print("Program exited")

replace the original string replace

S.replace(old,new[,count])

title to title format (change the first letter of each string to uppercase)

S.title()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324630759&siteId=291194637