A: string as an ordered data type, there are the following methods of operation:
1: Subscript (Index)
2: Slicing (including cutting)
3: Common built-in functions.
1) Index: A subscript is starting from 0. Interval:, 0=<index<len (str), from left to right to read.
One is starting from-1. The interval is len (str) =< index=<-1, read from right to left.
A= ' Come ' Print (a[0]) print (a[3]) print (a[-1]) print (a[-4])
If the index value exceeds the index of the string, it is thrown: indexerror.
2) Slice: Is the method of getting a set of elements of a split string. The index above can only get a single element from the subscript. Slices can get a specified set of elements. Syntax: Str[start:end:step] By default, 3 values can be omitted, and the default ellipsis step is 1. Str[:] Indicates that all elements of the string are taken.
Str[:end] represents the output of an element from a string that starts with a string to a end-1.
A= ' Come ' Print (a[1:]) print (A[1:3]) print (A[:3:2]) #表示步长是1, take one from the other. Omeomcm
3) The string is an immutable numeric type and can be used to obtain a new string from the corresponding slice, but it cannot be manipulated by Del, A[INDEX]=NEWSTR, and so on. Will be thrown: TypeError: ' str ' object does not the support item assignment.
1. Determine if the string has a numeric composition (Str.isdigit ()):
Application scenario: the input () function is converted to a string by default when it enters a number. You can tell whether a user is entering a number or a letter, and if it is a number, make the appropriate data type conversion. To perform the operation of the corresponding numeric operation after the program.
Age=input (' Your Age was: ') if Age.isdigit (): Age=int (age) Print (type) else:print (' You entre invail!} E num! ') Your age is:qyou entre invail!! Entre num!your age is:22<class ' int ' >
2: Remove whitespace from string or enter (Str.strip)
Scenario: When a user enters a string by using the input () function, the user is prevented from entering a space or a carriage return.
Age=input (' Your Age was: ') list_1=list (age) Age1=age.strip () list_2=list (age1) print (list_1) print (list_2) your age is:33 [' 3 ', ' 3 ', '] [' 3 ', ' 3 ']
3: Center Output string: str.entre (num, ' str '); Repeat output string: str*num
A= ' Welcome ' Print (A.center (+, ' # ')) ############### #welcome ################ #print (' # ' *30) ####################### #######
4: Member operation of the string, in or not in
A= ' Adde ' Print (' A ' in a)
5: String connection + or join or ' join ' (str)
It is best not to connect +, which consumes resources when the amount of data is large. The STR2=STR+STR1 connector opens up 3 spaces to store data at the same time in a memory space.
Preferably with join
A= ' Adde ' b= ' RRR ' Print (a+b) print (A.join (b)) print (' * '. Join (a)) adderrrradderaddera*d*d*e
6. The str.split (' symbol ') function indicates what string to cut. and returns a list. Sometimes when data conversion is used
A= ' adde,ddd ' Print (A.split (', ')) [' Adde ', ' DDD '
Other common functions:
1, String.capitalize () capitalize the first character of a string
2, Str.find (str) returns the position of the string where the specified string str is located.
3. String.isalnum () if the string has at least one character and all characters are letters or numbers, return
Returns TRUE, otherwise False is used to determine whether there are special characters in the string.
Python Basics-string