(python)字符串操作

python提供了几种不同的方式来表示字符串:

用单引号,双引号,或都三引号都可以表示一个字符串

>>> a1='monicx'
>>> a2="monicx"
>>> a3='''monicx'''
>>> type(a1)
<class 'str'>
>>> type(a2)
<class 'str'>
>>> type(a3)
<class 'str'>

字符串操作:

一、重点掌握

1、按索引取值:正数索引值为正向取字符,负数索引值为反向取字符

>>> name='monicx'
>>> name[-4]
'n'
>>> name[4]
'c'
>>>

2、切片(start:end:step)

>>> str='monicx is so handsome'
>>> str[0:6]
'monicx'
>>> str[0:14]
'monicx is so h'
>>> str[0:14:2]
'mnc ss '
>>> str[::-1]  #将字符串反序
'emosdnah os si xcinom'
>>> str[::-2]
'eoda ss cnm'
>>> str[0:-2]
'monicx is so handso'

3、长度

>>> str='monicx is so handsome'
>>> len(str)
21
4、 成员运算(in与not in )
>>> str='monicx is so handsome'
>>> 'z'in str
False
>>> 'a'in str
True
>>> 'monicx'not in str
False

5、移除空白(strip)

>>> str='   monicx is so handsome     '
>>> len(str)
29
>>> str=str.strip() #strip去除两边的空格
>>> len(str)
21
6、 切分split()正切分、 rstrip()反切分:

语法:字符串.sprit('按什么切分',次数)。切分一个字符串后会得到一个列表。

    按空格切分:

>>> str='monicx is so handsome'
>>> str.split(" ")
['monicx', 'is', 'so', 'handsome']

    按空格切分1次:

>>> str='monicx is so handsome'
>>> str.split(" ",1)
['monicx', 'is so handsome']

     按空格反切分1次:

>>> str='monicx is so handsome'
>>> str.rsplit(" ",1)
['monicx is so', 'handsome']

    按“i”切分:

>>> str='monicx is so handsome'
>>> str.split("i")
['mon', 'cx ', 's so handsome']

二、撑握操作

1、lstrip(),rstrip()

  lstrip()左边去空白,rstrip()右边去空白。

>>> str="   monicx is so handsome   "
>>> str.lstrip()
'monicx is so handsome   '
>>> str.rstrip()
'   monicx is so handsome'
2、lower(),upper()
lower()是将字符串中的所有的字符变小写,upper()则是变大写。
>>> str="Monicx Is So Handsome"
>>> str.upper()
'MONICX IS SO HANDSOME'
>>> str.lower()
'monicx is so handsome'

3、%占位符

>>> str="Monicx is so %s"%('handsome')
>>> str
'Monicx is so handsome'

延伸:

>>> c=('handsome','cool')
>>> str="Monicx is so %s"%(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> c=('handsome','cool')
>>> str="Monicx is so %s"%((c,))
>>> str
"Monicx is so ('handsome', 'cool')"

4、format()

format()它用{}来代替%占位符的型式,而且比%用的更加的灵活和强大。python3推荐使用。

它用三种用法:

第一种:

>>> str='my name is {} I am {} years old'.format('monicx',23)
>>> str
'my name is monicx I am 23 years old'

第二种:

>>> str='my name is {1} I am {2} years old'.format(43,'monicx',23)
>>> str
'my name is monicx I am 23 years old'

第三种:

>>> str='my name is {name} I am {age} years old {sex}'.format(name='monicx',sex='male',age=23,)
>>> str
'my name is monicx I am 23 years old male'
>>>

5、join()

join()就是把一个字符串用另外一个字符或字符串,插入到这个字符串每个字符之间

>>> str='monicx'
>>> '-'.join(str)
'm-o-n-i-c-x'

6、replace()

就是用一个字符串来代替这个字符串中的子串。

>>> str='monicx'
>>> str.replace('x','x is handsome')
'monicx is handsome'

7、isdigit()

判断一个字符串如果只包含数字则返回 True 否则返回 False。

>>> str1='monicx12'
>>> str2='12345678'
>>> str1.isdigit()
False
>>> str2.isdigit()
True
>>>

8、find(),rfind()

>>> str="Monicx Is So Handsome"
>>> str.find('z') #如果在没有找到就返回-1
-1
>>> str.find('o') #如果找到就返回找到第一个的索引
1
>>> str.find('o',2,24)
11
>>> str.rfind('o',2,24) #反序找到第一个
18

9、index(),rindex()

index()方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,该方法与find()方法一样,只不过如果str不在 string中会报一个异常。

>>> str="Monicx Is So Handsome"
>>> str.find('z')
-1
>>> str.index('z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

三、了解操作

1、zfill()

>>> str='monic'
>>> str.zfill(30)
'0000000000000000000000000monic'

2、rjust(),ljust()

>>> str='monic'
>>> str.ljust(30,'*')
'monic*************************'
>>> str.rjust(30,'*')
'*************************monic'

3、center()

>>> str='monic'
>>> str.center(30,'*')
'************monic*************'

4、count()

>>> str='monic is so handsome'
>>> str.count('i',0,30)
2

5、expandtabs()

把字符串中的‘\t’默认转化为8个空格

>>> str='monic\tis so handsome'
>>> str.expandtabs()
'monic   is so handsome'

6、captalize(),title()

>>> str='monic is so handsome'
>>> str.capitalize()
'Monic is so handsome'
>>> str.title()
'Monic Is So Handsome'

7、swapcase()

将字符串中大写转换为小写,小写转换为大写。

str='Monic Is So Handsome'
str.swapcase()

8、is数字系列

num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3=u'肆' #中文数字
num4=u'Ⅳ' #罗马数字

isdigit():bytes,unicode的阿拉伯数字为True
print(num1.isdigit())
print(num2.isdigit())
print(num3.isdigit())
print(num4.isdigit())

isdecimal():unicode的阿拉伯数字为True
print(num2.isdecimal())
print(num3.isdecimal())
print(num4.isdecimal())

#isnumberic:unicode的阿拉伯数字\中文数字\罗马数字为True
print(num2.isnumeric()) 
print(num3.isnumeric())
print(num4.isnumeric())
9、is其他
print('monicx is so handsome'.isalpha()) #字符全由字母组成
print('asdf'.isalnum()) #字符由字母或数字组成
print('I Am Monicx'.istitle())
print('    '.isspace())
print('print'.isidentifier())#是不是关键字







猜你喜欢

转载自blog.csdn.net/miaoqinian/article/details/80529420