python String Segmentation and Some Conventional String Methods

  • 2021-07-26 08:12:40
  • OfStack

String segmentation, which splits a string into a list composed of multiple strings, can be understood as string to list, which is often used

Syntax: str. split (sep, [, max]), sep can specify the symbol of the cut, max can specify the number of cuts (the number is not commonly used)

Split with spaces without parameters

When there is a parameter, the parameter is used for segmentation

When no delimiter is found, the list contains only the original string


source ="1,2,3,4,5,,6"
print source.split(',')
# Value by index []
source ="1,2,3,4,5,,6"
print source.split(',')[2]
 List to string  .join  Function, these two procedures are two opposite procedures, .join It is used very much 
source =['1','2','3','4','5','6']
print ','.join(source)

A few string methods that are not commonly used:

String case


 str.upper() -- Turn to capitalization 
  str.lower() -- Turn to lowercase 
  str.capitalize() -- Capital initials 
  str.istitle() -- Is the first letter capitalized  # Return bool Value 
  str.isupper() -- Are all letters capitalized # Return bool Value 
  str.islower() -- Are all letters lowercase  # Return bool Value 
 Application scenario: If the value passed by the user has both case and case during automated testing, the function of turning case may be used. 
  Strip spaces from strings  
  str.strip() -- Remove the left and right spaces of the string  
  str.lstrip() -- Remove the left space of the string 

  str.rstrip() -- Remove the right space of the string 
a =" ab sc "
print a.lstrip()
print a.rstrip()
print a.strip()
print a.replace(' ','')# Replace all spaces with nulls 
  Others 
  str.isalnum() -- Are they all letters and numbers, and at least have 1 Characters 
  str.isalpha() -- Are they all letters and at least have 1 Characters 
  str.isdigit() -- Are they all numbers and at least have 1 Characters  # More commonly used 
  str.isspace() -- Are all white space characters and at least have 1 Characters 
  str.count(targer,[min,max))  -- Statistics of the number of times a character appears in a string, which is commonly used. min max From which place to which place 
  str.startswith(target) -- Determine whether a string starts with a string  #a.startswith('name=')
  str.endswith(target) -- Determine whether a string ends with a string 

1. Given 1 string target = 'hello world' Find the first non-repeating character and output what bit it is


target = 'hello world'
for i in target:
  if target.count(i)==1:
    break
print i
print target.index(i)

Summarize


Related articles: