Python3基础-基本数据类型之数字、字符串1

基本数据类型

1、数字 int

python 2 int 有范围,超过该范围则为long 类型
32位系统上,取值范围为-2^31~2^31-1 ,即-2147483648~2147483647
64位系统上,取值访问为-2^63~2^63-1,即-9223372036854775808~9223372036854775807
python 3 所有整型都是int 类型,没有long类型

eg: 

#数字
input_num= input("请输入数字:")   #获取从键盘输入的数字
print("查看input_num的类型",type(input_num))  #查看类型  <class 'str'>
num= int(input_num)   # int() 将str类型转换为int类型.默认10进制 
print("查看num1的类型",type(num)) #查看类型  <class 'int'>

int() 默认值输入的英文则会报错

  File "D:/pyAuto/pythonDay/pythonbase/Day1.py", line 212, in <module>
    num= int(input_num)   # int() 将str类型转换为int类型.默认10进制
ValueError: invalid literal for int() with base 10: 'e'

 

python3中的数字默认进制为十进制,即python3默认10=9+1。
在python中二进制用0b加相应数字来表示,8进制用0o加相应数字来表示,16进制用0x加相应数字来表示。
数字转换10进制、2进制、8进制、16进制

#数字
input_num= input("请输入数字:")   #获取从键盘输入的数字
print("查看input_num的类型",type(input_num))  #查看类型
num= int(input_num)   # int() 将str类型转换为int类型.默认10进制
num1= int(input_num,base=2)  #转换为二进制
num2= int(input_num,base=8)  #转换为八进制
num3= int(input_num,base=16)  #转换为十六进制
print("查看num1的类型",type(num))
print("查看num的值=%d,查看num1的值=%d,查看num2的值=%d,查看num3的值=%d"%(num,num1,num2,num3))
执行结果如下:
请输入数字:00011111 查看input_num的类型 <class 'str'> 查看num1的类型 <class 'int'> 查看num的值=11111,查看num1的值=31,查看num2的值=4681,查看num3的值=69905

  

"""
因为十六进制已经超过10了。所以,十六进制从十开始用字母代替,即十进制的1-16表现为:1、2、3、4、5、6、7、8、9、A、B、C、D、E、F、10
"""
input_alpha= input("请输入英文:")  #输入A
num1= int(input_alpha,base=16)  
print("查看num的值=%d,"%(num1))  #则输出10

bit_length()

age=0
#0  0
#1  1
#2  10
#3  11
#4  100
#5  101
while age < 11:
    r = age.bit_length()  #当前数字的二进制,至少用n位表示
    print("age=%s,占用%s位"%(age,r))
    age += 1

"""
执行结果
ge=0,占用0位
age=1,占用1位
age=2,占用2位
age=3,占用2位
age=4,占用3位
age=5,占用3位
age=6,占用3位
age=7,占用3位
age=8,占用4位
age=9,占用4位
age=10,占用4位
"""

 

 

2、字符串 str

1.python官方定义中的字母:默认为英文字母+汉字即可
2.python官方定义中的数字:默认为阿拉伯数字+带圈的数字即可

#字符串
username=input("请输入纯英文字符串(小写或者大写):")
if username.isalpha():  #判断是否输入值是否为字母
    username1=username.upper()   #转换成大写
    username2=username.lower()   #转换成小写
    username3=username.capitalize()  #首字母换成大写,其他的字母换成小写
    username4=username.swapcase() #遇到大写变小写,遇小写变大写
    #lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法 例如德语 ß
    username5=username.casefold()
    # str.center(width[, fillchar])返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。
    # fillchar  只能一个字符,可有可无
    username6=username.center(10,'*')
    #str.count(sub, start= 0,end=len(string))
    #sub -- 搜索的子字符串
    #start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
    #end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
    username7=username.count("aa")
    #str.endswith(suffix[, start[, end]]) 如果字符串含有指定的后缀返回True,否则返回False。
    #类似 str.startswith(suffix[, start[, end]])
    #suffix -- 该参数可以是一个字符串或者是一个元素。
    #start -- 字符串中的开始位置。
    #end -- 字符中结束位置。
    username8=username.endswith('yu')
    print("原字符%s;转换成大写%s;转换成小写%s;首字母换成大写%s;遇到大变小,遇小变大%s;所有转换成小写%s"%(username,username1,username2,username3,username4,username5))
    print("center()返回的值%s;count()返回值%s;endswith()返回值%s"%(username6,username7,username8))
else:
    print("输入的不是纯英文的")

ljust() 和rjust

ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串

rjust() 方法返回一个原字符串右对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串

zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。

#ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串
test="窗前明月光"
test1=test.ljust(15,'$')
test2=test.rjust(15)
test3=test.zfill(15)
print(test1,test2,test3)

"""
窗前明月光$$$$$$$$$$
           窗前明月光 
0000000000窗前明月光
"""

 

isspace()

方法检测字符串是否只由空白字符组成。

test='123dd<>:"!@#$%^&*()'
test1="122\ndddd"
test2="12 2  3"
test3=""  #空字符
test4=" " #包含一个空格的字符
print(test.isspace(),test1.isspace(),test2.isspace(),test3.isspace(),test4.isspace())

"""
执行结果如下
False False False False True
"""

isalnum()

判断字符串中是否只包含 字母和数字

test='123 yuuu'  #数字、空格、字母
test1=test.isalnum()  #判断字符串中是否只包含 字母和数字
test='123yuuuSS' #数字、字母
test2=test.isalnum()
print("test1==%s;test2==%s"%(test1,test2))

"""
test1==False;test2==True
"""

isprintable()

判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空。
Unicode字符集中“Other” “Separator”类别的字符为不可打印的字符(但不包括ASCII码中的空格(0x20))。可用于判断转义字符。
ASCII码中第0~32号及第127号是控制字符;第33~126号是可打印字符,其中第48~57号为0~9十个阿拉伯数字;65~90号为26个大写英文字母,97~122号为26个小写英文字母

test='123dd<>:"!@#$%^&*()'
test1="122\ndddd"
print(test.isprintable(),test1.isprintable()) #是否存在不可显示的字符 \n 换行 \t 不可见

"""
执行结果
True False
"""

 

Istitle()和title()

Istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
title() 方法返回"标题化"的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写(见 istitle())。

test="Hi, My name is sugh"
test1="Hi, My Name Is Sugh"
test2="Hi, My Name Is Sugh,123"
print(test.title())
print(test.istitle(),test1.istitle(),test2.istitle())

"""
执行结果如下
Hi, My Name Is Sugh
False True True
"""

 

expandtabs(tabsize=8) 

expandtabs(tabsize=8) 把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8

#expandtabs(tabsize=8)  把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8
test_num="\tdddddddddddddddddddddddddd"
test_num1="ddddd\td"
test_num2="12345678\t9"
test=test_num.expandtabs(6)
test1=test_num1.expandtabs(6)
test2=test_num2.expandtabs(6)
test_num3="username\tpasswd\nsugh\t123456\n012345\tdddddd"
test3=test_num3.expandtabs(15)
print("test==%s;test1==%s;test2==%s"%(test,test1,test2))
print(test3)

"""
执行结果如下
test==      dddddddddddddddddddddddddd;test1==ddddd d;test2==12345678    9
username       passwd
sugh           123456
012345         dddddd
"""

isisdigit()、isdecimal() 、isnumeric()

isdigit()检测字符串是否只由数字组成
isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象
isnumeric()字符串是否只由数字组成,数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。指数类似 ² 与分数类似 ½ 也属于数字**

test='123'
test1=test.isdigit() #isdigit()检测字符串是否只由数字组成
test4=test.isdecimal() #isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象
test='123①'
test2=test.isdigit()
test5=test.isdecimal()
test='123①dddd'
test3=test.isdigit()
print("test1=%s;test2=%s;test3=%s;test4=%s;test5=%s"%(test1,test2,test3,test4,test5))

"""
执行结果如下
test1=True;test2=True;test3=False;test4=True;test5=False
"""
test=''
#isdigit()检测字符串是否只由数字组成
test1=test.isdigit()
#isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象
test2=test.isdecimal()
#isnumeric()字符串是否只由数字组成,数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。指数类似 ² 与分数类似 ½ 也属于数字
test3=test.isnumeric()
print("test1=%s;test2=%s;test3=%s"%(test1,test2,test3))

"""
执行结果如下
test1=False;test2=False;test3=True
"""

 

join()

方法用于将序列中的每个元素按照指定的字符进行拼接生成一个新的字符串

test="窗前明月光,疑是地上霜;举头望明月,低头思故乡"
join_num='@@'
test_num=join_num.join(test) 
print(test_num)

"""
执行结果如下
窗@@前@@明@@月@@光@@,@@疑@@是@@地@@上@@霜@@;@@举@@头@@望@@明@@月@@,@@低@@头@@思@@故@@乡
"""

test="窗前明月光,疑是地上霜;举头望明月,低头思故乡"
join_num='@@'
test_num=test.join(join_num)
print(test_num)

"""
执行结果如下
@窗前明月光,疑是地上霜;举头望明月,低头思故乡@
"""

lstrip()、rstrip()、strip()

lstrip()方法用于截掉字符串左边的空格或指定字符; 默认去除\n、\t、空白

rstrip()方法用于截掉字符串右边的空格或指定字符; 默认去除\n、\t、空白

strip()方法用于移除字符串头尾指定的字符(默认为空格)或字符序列; 默认去除\n、\t、空白

test="---111-222-333-444-555--"
print(test.lstrip('-')) #去除左边的指定字符
print(test.rstrip('-')) #去除右边的指定字符
print(test.rstrip('-45')) #去除右边的指定字符
print(test.rstrip('1-45')) #去除右边的指定字符
print(test.split('---')) #去除全部指定字符
print(test.split('-45')) #去除全部指定字符
print(test.split('222')) #去除全部指定字符

"""
执行结果如下
111-222-333-444-555--
---111-222-333-444-555
---111-222-333
---111-222-333
['', '111-222-333-444-555--']
['---111-222-333-444-555--']
['---111-', '-333-444-555--']
"""
test="     this is string example....wow!!!     "
print(test.lstrip()) #去除左边的空格
print(test.rstrip()) #去除右边的空格
print(test.split()) #去除全部空格

"""
this is string example....wow!!!     
     this is string example....wow!!!
['this', 'is', 'string', 'example....wow!!!']
"""

 

maketrans()、translate(table) 

maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
两个字符串的长度必须相同,为一一对应的关系。
translate(table) 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符,要过滤掉的字符放到 deletechars 参数中

str='this is string example....wow!!!'
temp=str.maketrans("tsWp","9876")
new_num=str.translate(temp)
print(new_num)

"""
执行结果
9hi8 i8 89ring exam6le....wow!!!
"""

partition() 、rpartition() 、split()、rsplit()、splitlines()

#partition() 方法用来根据指定的分隔符将字符串进行分割
#rpartition() 方法类似于 partition() 方法,只是该方法是从目标字符串的末尾也就是右边开始搜索分割符。。
#split() 通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串
#rsplit() 方法通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格、换行(\n)、制表符(\t)等。
#rsplit() 类似于 split() 方法,只不过是从字符串最后面开始分割。
#splitlines( keepends ) 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表
splitlines( keepends ) 如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

test_num="窗前1明月1光疑a1是地1上霜\n举头1望明月\t低1头思故乡1"
print(test_num.partition('1'))
print(test_num.rpartition('1'))
print(test_num.split('1'))
print(test_num.rsplit('1'))
print(test_num.splitlines(False))
print(test_num.splitlines(True))


"""
执行结果如下
('窗前', '1', '明月1光疑a1是地1上霜\n举头1望明月\t低1头思故乡1')
('窗前1明月1光疑a1是地1上霜\n举头1望明月\t低1头思故乡', '1', '')
['窗前', '明月', '光疑a', '是地', '上霜\n举头', '望明月\t低', '头思故乡', '']
['窗前', '明月', '光疑a', '是地', '上霜\n举头', '望明月\t低', '头思故乡', '']
['窗前1明月1光疑a1是地1上霜', '举头1望明月\t低1头思故乡1']
['窗前1明月1光疑a1是地1上霜\n', '举头1望明月\t低1头思故乡1']
"""

 

startswith()、endswith()

#startswith('窗前1') 以xx开头,符合条件则True;不符合条件,则False
#endswith('窗前1') 以xx结尾,符合条件则True;不符合条件,则False

test_num="窗前1明月1光疑a1是地1上霜\n举头1望明月\t低1头思故乡1"
print(test_num.startswith('窗前1'))
print(test_num.startswith('窗前11'))
print(test_num.endswith('故乡1'))
print(test_num.endswith('故乡2'))

"""
True
False
True
False
"""

isidentifier()

方法用于判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法。

print( "else".isidentifier())
print( "if".isidentifier())
print( "class".isidentifier())
print( "endswith".isidentifier())
print( "endswith()".isidentifier())
print( "_a".isidentifier())
print( "123add".isidentifier())
print( "123".isidentifier())
print( "aa".isidentifier())
print( "".isidentifier())
print( "②中国".isidentifier())

"""
执行结果如下
True
True
True
True
False
True
False
False
False
False
False

"""

 

posted @ 2019-10-12 11:50  槑槑DE  阅读(490)  评论(0编辑  收藏  举报