数值与字符串类型

Python3 中有六个标准的数据类型:Number(数字)、String(字符串)、List(列表)、Tuple(元组)、Sets(集合)、Dictionary(字典)。

  • 不可变数据(四个):Number(数字)、String(字符串)、Tuple(元组)、Sets(集合);
  • 可变数据(两个):List(列表)、Dictionary(字典)。

一、数字类型

 数字类型包括:int(整型)、float(浮点数)、complex(复数)、bool(布尔值)。

注:python2中还有一个长整型,python3已经没有了。

#a=10
#b=10.0
#c=True
#d=1+2j
#print(type(a))
#print(type(b))
#print(type(c))
#print(type(d))

运行结果如下:

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'complex'>

二、String 字符串类型:

  在单引号\双引号\三引号内,输入一串字符即构成一个字符串。例如,name='张三' 这样name这个变量就是字符串类型。单引号,双引号使用起来没有区别,三引号一般用于多行注释。
  字符串有很多常用的方法,经常使用的如下:

1、去除空格:
str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格
str.lstrip():删除字符串左边的指定字符,括号的写入指定字符,默认为空格
str.rstrip():删除字符串右边指定字符,默认为空格
'''
str.strip():删除字符串两边的指定字符,括号的写入指定字符,默认为空格
str.lstrip():删除字符串左边的指定字符,括号的写入指定字符,默认为空格
str.rstrip():删除字符串右边指定字符,默认为空格

'''
>>> a="    abc    "
>>> b = a.strip()    # b='abc'
>>> b = a.lstrip()       # b = 'abc     '
>>>b = a.rstrip()        #b =  '          abc'
View Cod
2、字符串的连接
“+”连接两个字符串,这个方法比较耗费资源,连接两个字符串就要开辟一次内存空间。。如果是n个字符串相连 那么会开辟n-1次内存。
str.join() 方法用于指定一个字符连接另外一个字符串。
#“+”
>>> a='hello'
>>> b='world'
>>> print(a+b)
helloworld

#“str.join()”
>>> a='H'
>>> b='world'
>>> c=a.join(b)
>>> c
'wHoHrHlHd'
View Code

 3、字符串的大小写转换

使用str.upper() str.lower()
 
>>> str1='abc'
>>> str2='FGH'
>>> print(str1.upper())
ABC
>>> print(str2.lower())
fgh
View Code
 

 4、format 的三种玩法:

>>> res1='{} {} {}'.format('egon',18,'male')
>>> res2='{1} {0} {1}'.format('egon',18,'male')
>>> res3='{name} {age}{sex}'.format(sex='male',name='egon',age=18)
>>> res1
'egon 18 male'
>>> res2
'18 egon 18'
>>> res3
'egon 18 male'
View Code

 5、split 的用法:

默认分隔符为空格。同时也可以指定切片的次数和切片方向。具体可以看下面示例

>>> name='root:x:0:0::/root:/bin/bash'
>>> print(name.split(':'))
['root', 'x', '0', '0', '', '/root', '/bin/bash']

>>> name='C:/a/b/c/d.txt'
>>> print(name.split('/',1))
['C:', 'a/b/c/d.txt']

>>> name='C:/a/b/c/d.txt'
>>> print(name.rsplit('/',1))
['C:/a/b/c', 'd.txt']
View Code

 6、replace的用法:

默认是替换全部匹配的地方,可以自己手动指定替换次数。
>>> shuaige='Panan:"Panan is the most hansome person in the world."'
>>> print(shuaige.replace('Panan','Dashu',1))
Dashu:"Panan is the most hansome person in the world."

>>> print(shuaige.replace('Panan','Dashu',))
Dashu:"Dashu is the most hansome person in the world."
View Code

7、isdigit的用法:

age=input('>>: ')
print(age.isdigit())

8.其他方法:

#1、find,rfind,index,rindex,count
#2、center,ljust,rjust,zfill
#3、expandtabs
#4、captalize,swapcase,title
#5、is数字系列
#6、is其他 
#find,rfind,index,rindex,count
name='egon say hello'
print(name.find('o',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
print(name.count('e',1,3)) #顾头不顾尾,如果不指定范围则查找所有

#center,ljust,rjust,zfill
name='hello'
print(name.center(30,'-'))
#------------hello-------------
print(name.ljust(30,'*'))   
#hello*************************
print(name.rjust(30,'*'))      #*************************hello
print(name.zfill(10)) #用0填充
#00000hello


#expandtabs
#默认‘\t’等于八个空格,参数可以自己指定个数
name='egon\thello'
print(name)
print(name.expandtabs(1))

#captalize,swapcase,title
print(name.capitalize()) #首字母大写
print(name.swapcase()) #大小写翻转
msg='egon say hi'
print(msg.title()) #每个单词的首字母大写

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

#isdigt:bytes,unicode
print(num1.isdigit()) #True
print(num2.isdigit()) #True
print(num3.isdigit()) #False
print(num4.isdigit()) #False

#isdecimal
#Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
#bytes类型无isdecimal方法
print(num2.isdecimal()) #True
print(num3.isdecimal()) #False
print(num4.isdecimal()) #False

#isnumberic:unicode,中文数字,罗马数字
#bytes类型无isnumberic方法
print(num2.isnumeric()) #True
print(num3.isnumeric()) #True
print(num4.isnumeric()) #True

#三者不能判断浮点数
num5='4.3'
print(num5.isdigit())
print(num5.isdecimal())
print(num5.isnumeric())
'''
总结:
    最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
    如果要判断中文数字或罗马数字,则需要用到isnumeric
'''

#is其他
name='egon123'
print(name.isalnum()) #字符串由字母或数字组成
print(name.isalpha()) #字符串只由字母组成
print(name.isidentifier()) #字符串里面只要是符合变量的规则返回TRUE
print(name.islower())    #包含字母,并且只有全部是小写才返回TRUE
print(name.isupper())   
print(name.isspace())   #检查是否只有空格
print(name.istitle())   #检查所有单词首字母是否大写
View Code

  

猜你喜欢

转载自www.cnblogs.com/linshuhui/p/8947556.html