码迷,mamicode.com
首页 > 编程语言 > 详细

python练习2

时间:2020-04-06 11:33:59      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:odi   直接   start   nbsp   case   map   包含   index   tar   

#!/usr/bin/env python
# -*- coding:utf-8 -*-

name = "郑建文"
"郑建文" 是字符串
""是字符


"""
# ctrl + ? 快捷键整体注释

if "z正" in name:
    print(‘OK‘)
else:
    print(‘Error‘)
    

if "才" not in name:
    print(‘不在‘)
else:
    print(‘在‘)

a = ‘文‘ not in name
print(a)
"""

数字有数字的功能
a = 123
v = a.bit_length()
print(v)

#字符串有字符串的功能
name1 = "shizhengwen"
v1 = name1.upper()
print(v1)

name2 = "LaiYing"
v2 = name2.upper()
print(v2)

int
str


a = "123"
print(type(a),a)

b = int(a)
print(type(b),b)

num = "0011"
v = int(num,base=2)
print(v)

num = "a"
v = int(num,base=16)
print(v)

age = 3
r = age.bit_length()
print(r)

#首字母大写
test = "alex"
v = test.capitalize()
print(v)


#所有大写变成小写,casefold更牛逼,很多未知的对应变小写
test = "aLex"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)

设置宽度,将内容居中
20代指总长度
* 代表填充,一个字符,可以为空
v = test.center(20,"*")
print(v)

在字符串中寻找包含ex,回馈出总的出现数量
test = "aLexalexr"
v = test.count("ex")
print(v)
test = "aLexalexr"
v = test.count("ex",5,10)
print(v)

欠
encode
decode

判断是否以e结尾
判断是否以al开头
test = "alex"
v = test.endswith("e")
v1 = test.startswith("al")
print(v)
print(v1)

欠
test = "12345678\t9"
v = test.expandtabs(6)
print(v,len(v))

从前往后找,找到第一个之后,获取该字符的位置
test = "alexalex"
未找到 -1
v = test.find("ex",2,8)
print(v)

index找不到,会直接报错,忽略
test = "alexalex"
v = test.index("exs")
print(v)

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

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

格式化,传入的值{"name":"alex","a":21}
test = i am {name},age{a}
v1 = test.format(name="ff",a=20)
v2 = test.format_map({"name":"alex","a":21})
print(v1)
print(v2)

判断字符串中是否只包含字母和数字
test = "aadd123-"
v = test.isalnum()
print(v)

 

python练习2

标签:odi   直接   start   nbsp   case   map   包含   index   tar   

原文地址:https://www.cnblogs.com/ykblogs/p/12640978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!