python对象类型----数字&字符串

一数据类型:
 
   float: 1.3e-3  1.3*10的负三次方
print (1.3e-3)

   bin()  #转换为二进进制
   oct() #转换为8进制
   hex()#转换为16进制

 数字类型的特点:
 1.只能存放一个值
 2.一经定义,不可更改
 3.直接访问
  x=10123123123
  print(id(x))
  x=11
  print(id(11))

二字符串:字符串类型:引号包含的都是字符串类型

  msg='hello'

 移除空白 msg.strip()
 分割msg.split('|')
 长度len(msg)
 索引msg[3] msg[-1]
 切片msg[0:5:2] #0 2 4

 x=x.strip()
 print(x)
 print(x.strip('*'))

#首字母大写
 x='hello'
 print(x.capitalize())

#所有字母大写
 x='hello'
 print(x.upper())

#居中显示
 x='hello'
 print(x.center(30,'#'))

#统计某个字符的长度,空格也算字符
 x='hel lo love'
 print(x.count('l'))
 print(x.count('l',0,4)) # 0 1 2 3

#判断是否以(‘’) 开头或结尾
x='hello '
 print(x.endswith(' '))
 print(x.startswith())

#是否能到到(‘’),结果为该字符的索引
 x='hello '
 print(x.find('e'))
 print(x.find('l'))

#格式化字符串
 msg='Name:{},age:{},sex:{}'
 print(msg) #Name:{},age:{},sex:{}
 print(msg.format('egon',18,'male'))

 msg='Name:{0},age:{1},sex:{0}'
 print(msg.format('aaaaaaaaaaaaaaaaa','bbbbbbbbbbbbbb'))

 msg='Name:{x},age:{y},sex:{z}'
 print(msg.format(y=18,x='egon',z='male'))


#根据索引找到对应的字符
 x='hello world'
 print(x[0])
 print(x[4])
 print(x[5])
 print(x[100]) #报错
 print(x[-1])
 print(x[-3])
 print(x[1:3])
 print(x[1:5:2])

 x='hello'
 print(x.index('o'))
 print(x[4])
 print(x[x.index('o')])

#判断是否为数字型字符串
 x='123'
 print(x.isdigit())

 age=input('age: ')
 if age.isdigit():
     new_age=int(age)
     print(new_age,type(new_age))

#替换字符串
 msg='hello alex'
 print(msg.replace('x','X'))
 print(msg.replace('alex','SB'))
 print(msg.replace('l','A'))
 print(msg.replace('l','A',1))
 print(msg.replace('l','A',2))


#补充:查看该命令的详细信息
 x='a' ---》 x=str('a')
 str.replace()

#字符串分片
 x='hello          world alex SB'
 x='root:x:0:0::/root:/bin/bash'
 print(x.split(':'))

#首字母大写
  x='hello'
 print(x.title())

#判断字母是否是大写或小写
 x='H'
 print(x.isupper())
 x='HELLO'
 print(x.islower())
 print(x.lower())

#判断是否是空格
 x='     '
 print(x.isspace())

#判断首字母是否大写
 msg='Hello'
 msg='hEEEE'
 print(msg.istitle())

#左对齐&右对齐
 x='abc'
 print(x.ljust(10,'*'))
 print(x.rjust(10,'*'))

#左右互换
 x='Ab'
 print(x.swapcase())
原文地址:https://www.cnblogs.com/mona524/p/6956662.html