python 数据类型简单介绍及有关 int,str的函数 day3

数据结构类型

int, str, bool, list, tuple, dict,

int 整形    例如0.1.2.3……等进行 数学运算的数字     eg: 任何非0 数转换为布尔值为True 0 为 Flase   int——>str     str (int())

str 字符串   例如  被‘’或“”引起来的被称为字符串       一般来说 字符串作为存储少量数据   字符串有这切片,索引    eg:任何非空字符串转换为布尔值为True 空为 Flase bool(str())

bool 布尔值     只有 逻辑真 True  或者逻辑假 Flase   

list 列表      存储大量数据  列表中可以嵌套列表格式为[   ,     ,     ,]   能存储任意类型的数据  

tuple 元组  又被称为 只读列表    只能进行 读取  不能修改 格式为 (   ,     ,       ,)

dict 字典   具有强烈的关系的类似于列表  有 key  和 value   格式为dict()

具体函数 

int的函数

s = 123
s1 = s.bit_length()
print(s1)
'''
将int类型转换为2进制,返回2进制的位数
'''

 

str   的函数

capitalize()

#字符串的操作
s = 'alexWUsir'
s1 = s.capitalize()  # 首字母大写
 print(s1)  # Alexwusir
'''
函数capitalize()
能将开头字母变成大写,其他均为小写 如果开头是数字则 开头不发生变换,剩余部分变成小写
Return a capitalized version of the string.
        
        More specifically, make the first character have upper case and the rest lower
        case.
'''

upper()  lower()

#upper() 函数 将所有的字符串变成大写字符串
#lower() 函数 将所以的字符串变成小写字符串 s = 's234ad1'
s_ = 'SssqD12' s2 = s.upper()
s3 = s_.lower()
print(s2) #S234AD1

print(s3) #sssqd12
'''
upper()
Return a copy of the string converted to uppercase.
lower()
 
  
Return a copy of the string converted to lowercase.
这俩函数 可以用在验证码转换

'''
s_str= 'acEQ1'
you_input =input('请输入验证码,不区分大小写')
if s_str.upper() == you_input.upper():
print('输入成功')
else:
print('请重新输入')
 

swapcase()

s = 'SssqD12'
s2 = s.swapcase()
print(s2) #sSSQd12
'''
大小写翻转
Convert uppercase characters to lowercase and lowercase characters to uppercase.

'''

title()

#titlie
s = 'tom,mary,ben'
s2 = s.title()
print(s2) # 返回Tom,Mary,Ben
'''
将每个单词的开头字母变成大写,其余的变成小写(如果遇到数字 特殊字符亦将转变成大写)
Return a version of the string where each word is titlecased(标题就是第一个字母大写)
More specifically, words start with uppercased characters and all remaining
cased characters have lower case
eg
s = 'fade,crazy*w4rri0r_songsong node_3'
s4 = s.title()
print(s4) #Fade,Crazy*W4Rri0R_Songsong Node_3

'''

center()

# center 将按照给定参数长度将字符串居中 默认填充是空格 可修改成其他字符串
s = '财务报表'
s2 = s.center(20,'*')
print(s2)   #  ********财务报表********
'''
Return a centered string of length width.
        
        Padding is done using the specified fill character (default is a space).
'''

expendtabs()

#expendtabs() 用于保留一定数量的空格
s = 'alex1\tsir'
s6 = s.expandtabs()
print(s6) # alex1   sir
'''
expendtabs()将\t之前的字符串进行取余,(8 - 得到的结果)就是所保留的空格
如上述例子中中间空3个空格  '\t'默认为8 也能进行修改 此函数可用在对齐文本中 显示的整齐
Return a copy where all tab characters are expanded using spaces.
        
        If tabsize is not given, a tab size of 8 characters is assumed.
'''

len()

#len()此为公共方法
s = 'alex瓜子'
s6 = len(s)
print(s6) # 6
'''
len()可以求出字符串,列表,元组等数据类型的元素个数
Return the number of items in a container.
'''

startswith()  endswith()

#endswith()判断某个字符串是否为指定字符串的后缀
s = 'alex瓜子'
s6 = s.endswith('',3,5)
print(s6)    #true
'''
endswith()默认设置stard end 为None 也可以进行切片同字符串操作相同
str = "this is string example....wow!!!"

suffix = "wow!!!"
print (str.endswith(suffix))
print (str.endswith(suffix,20))

suffix = "is"
print (str.endswith(suffix, 2, 4))
print (str.endswith(suffix, 2, 6))

Return True if S ends with the specified suffix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        suffix can also be a tuple of strings to try.

'''

find()   index()

# find ()寻找特定的字符串 并返回索引 否则返回-1
#index ()寻找特定的字符串 并返回索引 否则报错
s = '阿历克谢 ALXE'
s1 = s.find('A')
print(s1)   # 5
s2 = s.find('b')
print(s2)   # -1


s3 = s.index('a')
print(s3) #  ValueError: substring not found

strip()   lstrip()   rstrip()

#strip() 去除字符串两边的填充物  默认为空格,可以设置其他参数, 不可去除中间的填充物  lstrip()  去除左边的填充物  rstrip() 去除右边的填充物
s = '   hello   '
s1 = s.strip()
print(s1)

split()

#split() 将字符按照特定的规则分离 并转换为list  默认规则为空格  分离后 空格消失 
s = 'Tom Mary Ben'
s1 = s.split()
print(s1)

format()

#用于格式化输出
s = '我叫{},今年{},爱好{},再说一下我叫{}'.format('太白',36,'girl','太白') print(s) name = input('请输入名字:') s = '我叫{0},今年{1},爱好{2},再说一下我叫{0}'.format(name,36,'girl') print(s) name = input('请输入名字:') s = '我叫{name},今年{age},爱好{hobby},再说一下我叫{name}'.format(age=18,name=name,hobby='girl') print(s)

replace()

s = '少室山没有小小光头'
s1 = s.replace('','')
print(s1)   #少室山没有老老光头
#replace()为替代函数  可以选择替代次数int 默认为全部
s2 = s.replace('','',1)
print(s2) # 少室山没有老小光头

#is___系列

isdigit() 判断是否字符串为数字

isalnum() 判断字符串是否为字母或汉字或数字

isalpha() 判断字符串是否为字母或汉字等非特殊字符 非数字

转载于:https://www.cnblogs.com/hello-boy/p/10108146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值