python中字符串的基础方法

字符串用法 The use of String

一、字符串拼接 String concatenation
用法1(instruction):StringObject1+StringObject2

str1 = 'I am Iron man'
str2 = 'i am sprider man'
str3 = str1 + str2
print(str3)
print(str1,'and','str2')

用法2:字符.join(StringObject1)

str1 = '192.168.0.0'
print(':'.join(str1.split('.')))

######################
192:168:0:0

(split为字符串切割用法,在后面会补充说明)

二、字符串的重复 Repeat string
用法(instruction):StringObject*number

str1 = 'hello world'
print(str1*3)

三、用下表找对应字符(下标从0开始Use subscripts to find the string (the subscripts start with zero)
用法(instruction):StringObject[subscript]

str1 = 'china'
print(str1[2])

##########################
i

四、截取字符串片段 Intercepting string
用法(instruction):StringObject[start:stop]
1.从给定下标截取到终止下标前(from the subscript to the other subscript)

str1 = 'world'
print(str1[2:4])

#####################
rl

2.从头截到给定下标前(from the head of the string to the subscript)

str1 = 'world'
print(str1[:3])

#########################
wor

3.从给定下标截到结尾(from the subscript to the tail of the string)

str1 = 'world'
print(str1[1:])

################
orld

五、判断字符是否在对应字符串内 Determines whether a character in the string
用法(instruction):in StringObject/not in StringObject

str1 = 'world'
print('o' in str1)
print('ld' not in str1)

六、格式化输出 Formatted print

str1 = 'world'
print('str1=%s,num=%d'%(str1,2))

七、将字符串转化为有效的运算进行计算 Converts a string to a valid operation for evaluation
用法(instruction):eval(StringObject)

str1 = '12-3'
print(str1)
print(eval(str1))

######################
12-3
9

八、返回字符串长度 Return the length of the string
用法(instruction):len(StringObject)

str1 = 'world'
print(len(str1))

九、改变字符串大小写 Change the string case
1.全体大写/小写 (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.字符串单个首字母大写 (注意:和用法3区别)
instruction:StringObject.title()

str1 = 'hello world'
print(str1.title())

#########################
Hello World

十、找到对应字符串,返回值为下标 Find the corresponding string and return the string`s subscript
用法(instruction):StringObject.find(StringObject(start,stop))

str1 = 'world'
print(str1.find('or',0,len(str1)))

十一、截掉指定字符 (默认空格) Remove the specified character from the string (the default spaces)
用法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 

用法2:StringObject.split()(返回值为一个列表)

a = '192.168.0.0'
print(a.split())

##################
['192', '168', '0', '0']

十二、返回指定宽度位置字符串
用法(instruction):StringObject.center/ljust/rjust(width,fillchar)

str1 = 'world'
#居中
print(str1.center(20,'*'))
#左对齐
print(str1.ljust(30,'*'))
#右对齐
print(str1.rjust(40,'*'))

#####################
*******world********
world*************************
***********************************world

猜你喜欢

转载自blog.csdn.net/weixin_45717984/article/details/105098958