int及str方法的使用

python 类型有:整形int,字符串str,列表list,元祖tuple,字典dict,布尔值bool
a ='10'
print(type(a),a)
b=int (a) #将字符串转为int类型,使用type可以查看类型
print(type(b),b)

str:
1.count() 去字符串中寻找,寻找子序列的出现次数,如下
name="root"
text=name.count("r")
print(text) 结果:1

2.capitalize() 首字母大写
name ="root"
text=name.capitalize()
print(text) 输出结果为Root
3.casefold() 所有字母小写
name ="ROOT"
text=name.casefold()
print(text) 输出结果为root

4.center()内容居中
name ="ROOT"
text=name.center(10,'"')# 10代表总长度
print(text) 输出结果为:"""ROOT"""

5.startswith以什么开始,endswith已什么结束,返回为true或flase
name ="ROOT"
text=name.startswith("T")
text1=name.endswith("T")
print(text) #输出结果为flase
print(text1)#输出结果为true

6.*****find 从开始往后找,找到第一个之后,获取其未知

 test = "alexalex"# 未找到 -1
 v = test.find('ex')
 print(v) #输出结果为2

7.****format 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v)

test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v)

# 格式化,传入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}'
v1 = test.format(name='df',a=10)
v2 = test.format_map({"name": 'alex', "a": 19})

# 字符串中是否只包含 字母和数字
test = "123"
v = test.isalnum()
print(v)
# expandtabs(),断句20,
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
v = test.expandtabs(20)
print(v) #结果以表格形式输出

# 判断是否是字母,汉字
test = "as2df"
v = test.isalpha()
print(v) 输入false

#当前输入是否是数字
test = "二" # 1,②
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3) 输出False False True

# 是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v)输出false
# 判断是否全部是空格
test = " "
v = test.isspace()
print(v) 输出true

#  判断是否是标题
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1) #输出false
v2 = test.title()
print(v2) #将上面句子中所有首字母大写,输出结果:Return True If All Cased Characters In S Are Uppercase And There Is
v3 = v2.istitle()
print(v3) #输出true

#  ***** 将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
# t = ' '
v = "_".join(test)
print(v) 输出:你_是_风_儿_我_是_沙


# 18 判断是否全部是大小写 和 转换为大小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) 输出:false alex
v1 = test.isupper()
v2 = test.upper()
print(v1,v2) 输出:false AEX


# 去除左右空白
test="aa"
v = test.lstrip()
v1 = test.rstrip()
v2 = test.strip()
print(v,v1,v2)

#  分割为三部分
test = "testasdsddfg"
v = test.partition('s')
print(v) 输出:('te', 's', 'tasdsddfg') 从左往右分割
v = test.rpartition('s')
print(v) 输出:('testasd', 's', 'ddfg') 从右往左按s进行分割

# 22***** 分割为指定个数
test = "testasdsddfg"
v = test.split('s',2)
print(v) 输出:['te', 'ta', 'dsddfg']
 

转载于:https://www.cnblogs.com/yxks/p/10810413.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值