White school Python-Day09

1. String operations

  ① repeatedly output string

  print("hello" * 2)

  ② slicing operation, the same list

  print("hello" [1:])

  ③in determines whether elements in strings, the list is the same

  print("e" in "hello")

  ④ formatted output

  name = "kathrine"

  print("%s is a good boy" % name)

  ⑤ string concatenation is not recommended.

2. String built-in method

  ①join method, string concatenation, before join a string or an empty string, the content of the list or add a tuple format.

  d = "000".join(["a","b"])

  ②.count method, the number of an element in the string.

  print("hello".count("l"))

  ③.capitalize string capitalized

  print("hello".capitalize())

  ④..center (total number of characters, "other content except the content center"), center

  print("hello".center(50,"-"))

  ⑤.startswith, in order to determine whether an element is a start, can be a range, it returns a Boolean value.

  print("hello".startswith("he"))

  ⑥ added \ T (tab character) in the string, it can be used to set the number of spaces .expandtabs tabs.

  print("he\tllo".expandtabs(5))

  ⑦.find lookup index value of a certain element in the string, and return the index value, as this element does not exist, returns -1.

  print("hello".find("h"))

  Another way ⑧.format / .format_map formatted output

  format method

  The formatting output with the parameters of the position in the format process.

  name = "my name is {0},my age is {1}-{0}"

  val = name.format ("kathrine",20)

  print(val)

  If the parameters in parentheses format is a list, to add *

  name = "my name is {0},my age is {1}-{0}"

  val = name.format ( * ["kathrine",20])

  print(val)

  The format parameter can be a method of direct variable assignment

  nAme = input(">>:")

  info = "my name is {name},age is {age}"

  val = info.format(name = nAme , age =18 )

  print(val)

  The format argument is a case of dictionaries, with two **

  name = "My name is {name}, the age {age}"

  dic = {'name':'kathrine','age':18}

  val = name.format(**dic)

  print(val)

  format_map method parameter must be a dictionary

  info = "my name is {name},age is {age}"

  dic = {"name":"kathrine","age":19}

  info.format_map val = (say)

  print(val)

  Note: The dictionary will be reflected in a subsequent essay.

  ⑨index string index value of a certain element, as this element is not included, it is given

  print("hello".index("h"))

  ⑩isalnum determine whether the string contains letters or numbers, if they contain spaces or special characters will return an error value

  print("hello12345".isalnum())

  11.isdecimal determine whether a decimal

  print("hello123".isdecimal())

  12 detects whether a string consisting only of letters

  print("h123".isalpha())

  13 determines whether the string is an integer number

  print("123.111".isdigit())

  print("123.11".isnumeric())

  14 variables to determine whether it is illegal

  print("123ggg'.isdentifier())

  15 checks whether all lowercase letters

  print("123ggg'.islower())

  16 check whether all uppercase letters

  print("123ggg'.isupper())

  17 is a blank check whether

  print( " ".isspace())

  18 to determine whether the title, the title is characteristic of each word capitalized

  print("Abc Def".is title())

  print("abc Def".is title())

  19 string in all uppercase to lower case

  print("abcQW12".lower())

  20 all lowercase string becomes uppercase

  print("abcQW12"。upper())

  21 and the center is similar, but the string on the left

  print("hello".ljust(50,"*"))

  22 and the center is similar, but the string on the right

  print("hello".rjust(50,"*"))

  23 before and after removing the string line breaks and tabs spaces

  .strip strings before and after the newline and tab and remove the spaces (\ n newline)
   .lstrip the left of the string line breaks and tabs is removed and spaces (\ n newline)
   .rstrip the right of the string newline and tab and remove the spaces (\ n newline)

  print(" abc Def ".strip())

  print(" abc Def\n ".strip())

  Alternatively content 24, .replace ( "content to be replaced," "desired content", replace several values)

  print("hello".replace("h","f",1))

  25 real index position

  print("hello".rfind("e"))

  26 split into a string list to an element in the string of target partition, the partition object will not be output

  print ( "ab cdef" .split ( "")) from the space Split

  print ( "ab cdef" .split ( "b")) from the split b

  27 from right to left split rsplit ( "Where minute", several times)

  print("abc bws webs".rsplit("b",1))

  28 string becomes the title format

  print('abc bef.title()')

  

Guess you like

Origin www.cnblogs.com/Kathrine/p/11955663.html