The use of String
1. String concatenation String concatenation
usage 1 ( instruction ): StringObject1+StringObject2
str1 = 'I am Iron man'
str2 = 'i am sprider man'
str3 = str1 + str2
print(str3)
print(str1,'and','str2')
Usage 2: character.join(StringObject1)
str1 = '192.168.0.0'
print(':'.join(str1.split('.')))
######################
192:168:0:0
(split is the usage of string cutting, which will be explained later)
2. Repeat string
usage ( instruction ): StringObject*number
str1 = 'hello world'
print(str1*3)
3. Use the following table to find the corresponding characters (the subscript starts from 0 ) Use subscripts to find the string (the subscripts start with zero)
Usage ( instruction ): StringObject[subscript]
str1 = 'china'
print(str1[2])
##########################
i
4. Intercepting string fragments Intercepting string
Usage ( instruction ): StringObject[start:stop]
1. Intercept from the given subscript to before the termination subscript ( from the subscript to the other subscript)
str1 = 'world'
print(str1[2:4])
#####################
rl
2. Cut from the head to before the given subscript ( from the head of the string to the subscript )
str1 = 'world'
print(str1[:3])
#########################
wor
3. From the given subscript to the end ( from the subscript to the tail of the string )
str1 = 'world'
print(str1[1:])
################
orld
5. Determine whether a character is in the corresponding string Determines whether a character in the string
Usage ( instruction ): in StringObject/not in StringObject
str1 = 'world'
print('o' in str1)
print('ld' not in str1)
Six, formatted output Formatted print
str1 = 'world'
print('str1=%s,num=%d'%(str1,2))
7. Convert a string to a valid operation for evaluation Converts a string to a valid operation for evaluation
Usage ( instruction ): eval(StringObject)
str1 = '12-3'
print(str1)
print(eval(str1))
######################
12-3
9
8. Return the length of the string Return the length of the string
Usage ( instruction ): len(StringObject)
str1 = 'world'
print(len(str1))
Nine. Change the string case Change the string case
1. All uppercase/lowercase ( upper/lower )
instruction : StringObjet.upper()/StringObject.lower()
str1 = 'WOrld'
print(str1.upper())
print(str1.lower())
2. The string case interchange
instruction : StringObject.swapcase()
str1 = 'WOrld'
print(str1.swapcase())
3. Capitailze the first character
instruction : StringObject.capitalize()
str1 = 'hello world'
print(str1.capitalize())
#########################
Hello world
4. Capitalize the first letter of a single string (note: it is different from usage 3)
instruction : StringObject.title()
str1 = 'hello world'
print(str1.title())
#########################
Hello World
10. Find the corresponding string, the return value is the subscript Find the corresponding string and return the string`s subscript
Usage ( instruction ): StringObject.find(StringObject(start,stop))
str1 = 'world'
print(str1.find('or',0,len(str1)))
11. Cut off the specified character (default space) Remove the specified character from the string (the default spaces)
Usage 1 ( instruction ): StringObject.strip/lstrip/rstrip
str1 = ' h ell o wor l d !!'
print(str1.strip())
#截掉最左侧指定字符
print(str1.lstrip(' '))
#截掉最左侧指定字符
print(str1.rstrip('!'))
###############################
h ell o wor l d !!
h ell o wor l d !!
h ell o wor l d
Usage 2: StringObject.split() (the return value is a list)
a = '192.168.0.0'
print(a.split())
##################
['192', '168', '0', '0']
12. Return the specified width position string
Usage ( instruction ): StringObject.center/ljust/rjust(width,fillchar)
str1 = 'world'
#居中
print(str1.center(20,'*'))
#左对齐
print(str1.ljust(30,'*'))
#右对齐
print(str1.rjust(40,'*'))
#####################
*******world********
world*************************
***********************************world