python数字和字符串


#数字int
a="1223"
print(int(a))

n="0011"
print(int(n,base=2))

#bit_length()表示转化为2进制的位数
m=5
print(m.bit_length())

#字符串

# #把首字母变为大写
# test="shijs"
# v=test.capitalize()
# print(v)

#把字符中大写的变为小写,cadefold()更为强大,可以含多国语言
# test="hEllo"
# v=test.casefold()
# v1=test.lower()
# print(v,v1)

#计算在字符串中含有几个l字符
# test="hello world"
# m=test.count("l")
# print(m)

#以什么开头
#以什么结尾
# test="hello world"
# v=test.endswith("d")
# v2=test.startswith("e")
# print(v,v2)

#设置固定长度,并居中
#空位以@填充
# test="hello"
# m=test.center(20,"@")
# print(m)


#以6个字符断句,如果遇到\t就用空格补齐6个字符
# test="sjkj\tsk"
# m=test.expandtabs(6)
# print(m)

#从开始找,找到获取序号,后面不再找
# test="hello world"
# m=test.find("l")
# print(m)

#格式化,将字符串中的占位符替换为指定字符
# test="who is {name},age is {num}"
# m=test.format(name="liuwen",num=18)
# print(m)


# test="who is {0},age is {1}"
# m=test.format("liuwen",18)
# print(m)
#以字典的格式替换
# test="who is {name},age is {a}"
# m=test.format_map({"name":"liuwen","a":18})
# print(m)

#字符串中是否只含数字和字母
# test="sajfk@123"
# m=test.isalnum()
# print(m)

#判断字符串中是否只含有字母和中文
# test="saksa中jfk"
# m=test.isalpha()
# print(m)

#判断字符串中是否只含有数字,isdigit更加强大
# test="3825329"
# m=test.isdecimal()
# m1=test.isdigit()
# print(m,m1)

#判断是否是标识符,如_is,def class
# test="def"
# m=test.isidentifier()
# print(m)

#判断是否是小写
# test="sjsfU"
# m=test.islower()
# print(m)


#判断是否是数字.包括一 二数字都可以识别
# test="1233一"
# m=test.isnumeric()
# print(m)

#是否可以打印,当含有\t,\n时,不能打印
# test="skdja\najk"
# m=test.isprintable()
# print(m)

#是否都是空格
# test="  "
# m=test.isspace()
# print(m)

#是否是标题
# test="heoll world"
# m=test.istitle()
# n=test.title()
# v=n.istitle()
# print(m,n,v)

#是否是大写
# test="hello world"
# m=test.isupper()
# n=test.upper()
# v=n.isupper()
# print(m,n,v)

#通过#连接字符串
# test="小敏是最美"
# l="#"
# m=l.join(test)
# #m="#".join(test)
# print(m)
#小#敏#是#最#美

#固定长度20,把字符串放左边,后面用*填充
# test="hello"
# m=test.ljust(20,"*")
# print(m)

#固定长度20,把字符串放右边,后面用*填充
# test="hello"
# m=test.rjust(20,"*")
# print(m)

#把字符串转换为小写
# test="hSDJ"
# m=test.lower()
# print(m)

#把字符串左边的空格去除
# test="  sdkdkjf "
# m=test.lstrip()
# print(m)
#
# #把字符串右边的空格去除
# test="  sdkdkjf  "
# m=test.rstrip()
# print(m)
#
# #把字符串左右边的空格去除
# test="  sdkdkjf  "
# m=test.strip()
# print(m)

#创建对应关系和调用
# test="aouei"
# men ="12345"
# v="shafhjsfdoiue"
# m=str.maketrans(test,men)#创建对应关系
# new_v=v.translate(m)#按照对应关于替换
# print(new_v)

#按照s分割字符串,只能把字符串分割为三段
# test="dfsafhjhfksfdhfjdgkss"
# m=test.partition("s")
# print(m)

#按照s分割字符串,只能把字符串分割为三段
# test="dfsafhjhfksfdhfjdgkss"
# m=test.rpartition("s")
# print(m)

#按照s分割字符串,但是不能获取到s字符,且可以填分割次数
# test="dfsafhjhfksfdhfjdgkss"
# m=test.split("s",3)
# print(m)
#
# test="sfks"
# test.rsplit()#从右开始分割

#把小写转化为大写.把大写转化为小写
# test="sajkfkEIJISJ"
# m=test.swapcase()
# print(m)

#替换字符,指定替换个数
# test="ehjhsfsdjfei"
# m=test.replace("s","@",2)
# print(m)

#字符串索引
# test="小明同学"
# m=test[0:-1]#表示到倒数第二的字符
# # #test[:]代表所有字符
# print(m)

#计算字符串长度
# test="手机放房间"
# print(len(test))
# 5


#for循环
# test="小明同学你好"
# for i in test:
#     break
# continue

# print(i)

 

 

 

posted @ 2020-04-29 18:16  小文-520  阅读(338)  评论(0编辑  收藏  举报