python练习2

Posted AMKOOL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python练习2相关的知识,希望对你有一定的参考价值。

#!/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	9"
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的主要内容,如果未能解决你的问题,请参考以下文章

Python3练习题系列(03)

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Python程序设计 实验2:Python基础练习

python练习代码

csharp Epicor标准练习片段

golang 去练习片段