Python学习第四课——基本数据类型一之int and str

1、数字(int)

- int() 方法
  # 定义
    a1=123  
    a2=456
#功能1:将字符串转换为数字
#例子1:
a = "123"
print(type(a)) # type()为查看类型,此行输出结果为<class 'str'>
b =int(a)
print(type(b)) # type()为查看类型,此行输出结果为<class 'int'>
c=456
d=c+b
print(d) #输出结果为579

#功能2:按照进制转化输出
#例子2:
num="a"
b=int(num,base=16)  # int(?,base=?),前者则是参数,后者是按照几进制进行转换输出,此行为将num按照16进制输出
print(b) # 输出结果为 10

num1="0011"
v=int(num1,base=2)  # int(?,base=?),前者则是参数,后者是按照几进制进行转换输出,此行为将num1按照2进制输出
print(v)  # 输出结果为 3

 

- bit_length() 方法
age = 1   # 1 二进制=1
age1 = 2  # 1 二进制=10
age2 = 3  # 1 二进制=11
age3 = 4  # 1 二进制=100
age4 = 10  # 10  二进制=1010
r1 = age.bit_length()   # 当前数字的二进制,至少用几位来表示
r2 = age1.bit_length()  # 当前数字的二进制,至少用几位来表示
r3 = age2.bit_length()  # 当前数字的二进制,至少用几位来表示
r4 = age3.bit_length()  # 当前数字的二进制,至少用几位来表示
r5 = age4.bit_length()  # 当前数字的二进制,至少用几位来表示
print(r1)   # 输出结果为 1
print(r2)   # 输出结果为 2
print(r3)   # 输出结果为 2
print(r4)   # 输出结果为 3
print(r5)   # 输出结果为 4

2、字符串(str)

 

name = "hAnhAn"
v = name.capitalize()  # 将首字母大写
print(v)  # 输出结果 Hanhan

v1=name.casefold() # 将所有变为小写,但是这个方法更牛逼,因为很多未知的对应小写也可以实现
print(v1)  # 输出为 hanhan
v2=name.lower()   # 将所有变为小写
print(v2)  # 输出为 hanhan


v = name.center(20) # def center(self, width, fillchar=None) 将name放到参数width中间
print(v) # 输出结果为       hAnhAn
v0 = name.center(20,"*") # def center(self, width, fillchar=None) 
#将name放到参数width中间,前后空白位置用*补上(只能填一个字符)
print(v0) # 输出结果为 *******hAnhAn*******

v3=name.count("A")  # 计算参数在字符串name中的个数
print(v3)   # 输出结果为 2
v4=name.count("n")  # 计算参数在字符串name中的个数
print(v4)   # 输出结果为 2
v5=name.count("An",3) #def count(self, sub, start=None, end=None) 
#start表示从第几个往后面开始找
print(v5)   # 输出结果为 1


test="python"
v=test.endswith('on')  # 是否以括号中参数结尾
print(v) # 输出结果为 True
v1=test.startswith('h')  # 是否以括号中参数开头
print(v1) # 输出结果为 False

 

test="pythonpy"
v1=test.find("py")  # 前面往后面找,返回找到的位置。注:只能找到一个
print(v1) # 输出结果为 0


test="pythonpy"
# def find(self, sub, start=None, end=None) 后面两个参数是查找的范围
v2=test.find("py",5,8)  
print(v2) # 输出结果为 6

test="i am {name}"
print(test)
# 格式化,将一个字符串中的占位符替换为指定的值
v3=test.format(name="hanhan")  
print(v3)  # 输出结果为 i am hanhan


test='i am {name},age {a}'
print(test)
# 格式化,将一个字符串中的占位符替换为指定的值
v4=test.format(name="hanhan",a=23)  
print(v4)  # 输出结果为 i am hanhan,age 23


test='i am {0},age {1}'
print(test)
v5=test.format("hanhan",23)  # 格式化,对应数字进行赋值,可以一直写下去
print(v5)  # 输出结果为 i am hanhan,age 23

test='i am {name},age {a}'
# 格式化,以字典的形式往里面赋值
v6=test.format_map({"name":'hanhan',"a":23}) 
print(v6)  # 输出结果为 i am hanhan,age 23


test1="haha895-++"
v8=test1.isalnum() # 判断字符串是否由字母 数字组成
print(v8)  # 输出结果为False


test1="haha895"
v7=test1.isalnum() # 判断字符串是否由字母 数字组成
print(v7)  # 输出结果为Ture

test2="dhsjdh	565"
# 从前面找tab,六个六个的进行查找,直到找到,找到如果不够六个就用空格填补
v9=test2.expandtabs(6)
print(v9) # 输出结果为 dhsjdh      565

# 可以用expandtabs()做表格
test2="username	email	password
hanhan	han@q.com	123
hanhan	han@q.com	123
hanhan	han@q.com	123
hanhan	han@q.com	123"
print(test2)
# 输出结果为(未对齐):
# username    email    password
# hanhan  han@q.com    123
# hanhan  han@q.com    123
# hanhan  han@q.com    123
# hanhan  han@q.com    123
v10=test2.expandtabs(15)
print(v10)
# 输出结果
# username       email          password
# hanhan         han@q.com      123
# hanhan         han@q.com      123
# hanhan         han@q.com      123
# hanhan         han@q.com      123

 

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


test="wtudbsajbcaskdusaincuyhdas"
m = test.maketrans("abcdef","123456") # 将test字符串中的元素按照要求进行替换
v = test.translate(m)
print(v) #输出结果 wtu42s1j231sk4us1in3uyh41s

test="testhanhansdd"
v = test.partition('s') # 从前往后面找字符串中的第一个‘s’ 进行分割
print(v) # 输出结果为 ('te', 's', 'thanhan')

v1 = test.rpartition('s')  # 从后往前面找字符串中的第一个‘s’ 进行分割
print(v1) # 输出结果为 # 输出结果为

v2 = test.split('s')  # 从前往后面见到‘s’ 就进行分割
print(v2) # 输出结果为 ['te', 'thanhan', 'dd']

v3 = test.split('s',1)  # 从前往后根据后面的参数的个数进行分割
print(v3) # 输出结果为 ['te', 'thanhansdd']

 

 

test="hanhan"
v = test[0]  # 按照下标拿字符串
v1 = test[2]  # 按照下标拿字符串
print(v,v1)  # 输出结果 h n
v3 = test[0:2]  # 范围大于等于0小于2
print(v3)  #    输出ha
v4 = len(test)  #  获取字符串长度
print(v4)  # 输出结果 6


# 字符串循环输出(while循环)
index=0
while index < len(test):
    print(test[index])
    index += 1
print("-----------------")
# 字符串循环输出(for循环)
for item in test:
    print(item)

test1="etysbbdjsbn"
v = test1.replace("b",'456') #  def replace(self, old, new, count=None) 后者替换前者
print(v) # 输出结果为 etys456456djs456n

test1="etysbbdjsbn"
v1 = test1.replace("b",'456',1)#  def replace(self, old, new, count=None) 根据后面的数量后者替换前者
print(v1) # 输出结果为 etys456bdjsbn

# range帮助创建一定范围的数字
v=range(20)
for item in v:
    print(item)  #  输出结果 0,1,2,3,...19
v1=range(0,20,5)
for item1 in v1:
    print(item1) #  输出结果 0 5 10 15

# 打印文件索引
t=input("请输入:")
l= len(t) # 首先获取字符串长度
t1=range(0,l) # 利用range生成字符串下标
for it in t1:
    print(it,t[it]) #  通过for循环输出下标和下标对应的元素
# 简单写法
t=input("请输入:")
for it1 in range(0, len(t)):
    print(it1,t[it1])

 

 

 

原文地址:https://www.cnblogs.com/pyhan/p/12017355.html