(Self-developed artificial intelligence) python string, artificial intelligence python
A string is the most commonly used data type in python. We can use single quotation marks ('') or double quotation marks (" ") to create a string.
a='Hello'b="Hello"
All standard sequence operations such as (index, sharding, membership, length, minimum value and maximum value) are also applicable to strings.
Common string formatting symbols:
(% S formatted string)
Print ('hello, % s' % 'World') # Use % s as the placeholder for 'World' Hello, world # result print ('and % s Day' % 10) # using % d as the placeholder for 10 is still 10 days # result # % s can not only format the string, but also format the integer
(% D format integer)
Print ('and % d Day' % 10) # Use % d as a placeholder for 10 and there are 10 days # result
Common Methods for string:
Find (): used to check whether a string contains a substring 'str'. You can specify the start and end ranges.
A = 'hello, world' print (. find ('wo') 6 # returns the index print (. find ('kb')-1 # If no value is found,-1 is returned.
Print (a. find ('wo', 3) # provide the starting point 6 # result print (a. find ('wd ', 6)-1 # result
Print (a. find ('Wo ',) # provide the start and end 6 # result print (a. find ('wd',)-1 # result
Lower (): converts all uppercase characters in a string to lowercase letters.
A = 'hello' B = a. lower () print (B) HeLlo # result
Upper (): converts all lowercase characters in a string to uppercase letters.
A = 'hello' B = a. upper () print (B) HeLlo # result
Swapcase (): converts all lowercase characters in a string to uppercase and lowercase letters to lowercase letters.
A = 'hello' B = a. swapcase () print (B) HeLlo # result
Replace (): replace the old string in the string with the new string.
A = 'Hello world' B = a. replace ('hello', 'Hello') print (B) hello world # result
Strip (): removes the specified character from the beginning and end of a string.
A = '++ hello world ++' B =. strip ('+') print (B) hello world # Result B =. strip ('++ H') print (B) ello world # Result B =. strip ('+ + D') print (B) hello worl # result