首页 > 编程语言 > 详细

python入门二: 字符串1

时间:2020-01-19 15:47:37      阅读:66      评论:0      收藏:0      [点我收藏+]
关于字符串

数字类型转换:

print(bin(100)) #转成2进制
print(oct(100)) #转成8进程
print(hex(100)) #转成16进制

#结果
0b1100100     
0o144
0x64

# 0b 0o 0x 分别代表2 8 16 进制

字符串类型

msg='hello xx'
print(msg[-3],msg[3])
print(type(msg))
print(msg[:3])     #第0,1,2 (顾头不顾尾)
print(msg[5:0:-1]) #部分倒排
print(msg[-1::-1]) #倒排
#字符串类型可以像列表一样来取值
#结果
x l
<class 'str'>
hel
 olle
 xx olleh
#-1代表着倒数第一个

成员运算(判断一个字符串是否在一个大的字符串里面)

msg='hello'
msg1='hello world'
print(msg in msg1)
print(msg not in msg1)

#结果
True
False

去除空白strip

#调用方法,字符串类型下面的strip方法
x='  kong ge '
print(x.strip()) #默认是空格
y=' xxxxkong+gexxx'
print(y.strip(" x"))
#结果
kong ge
kong+ge
#这个方法是去除左右两边的,中间的不予处理(碰到第一个“”外的字符,自动跳过)
###
方法lstrip 去除左边的
方法rstrip 去除右边的

切分spilt

方法spilt
msg='root:x:0:0:/root/bin:x'
print(msg.split(":")[1])
#结果
x
#可以配置解压赋值来使用,分别赋值给各个变量

字符串处理方法

lower 转换为小写
upper 转换为大写
startswith 开头,返回布尔值
endswith 结尾
rspilt 从右切 和指定个数-1是一样的
lspilt 从左切


msg='hello World'
print(msg.lower())
print(msg.upper())
print(msg.startswith('he'))
print(msg.endswith('ld'))
print(msg.endswith('he'))
#结果
hello world
HELLO WORLD
True
True
False

字符串的格式化format

name='dhc'
age=18
print('my name is %s , my age is %s'%(name,age))#常见的
print('mys name is {} , my age is {}.'.format(name,age)) #按照顺序传变量
print('my name is {age},my age is {age}'.format(age=age,name=name))#指定传入
#结果
my name is dhc , my age is 18
mys name is dhc , my age is 18.
my name is 18,my age is 18

合并join

#有拆分spilt,自然就有合并join
msg='logstash:x:987:985:logstash:/usr/share/logstash:/sbin/nologin'
l=msg.split(':')
print(l)
print(':'.join(l))

#结果
['logstash', 'x', '987', '985', 'logstash', '/usr/share/logstash', '/sbin/nologin']
logstash:x:987:985:logstash:/usr/share/logstash:/sbin/nologin

替换replace

msg='i have a pen , i have two pen'
print(msg.replace('pen','haha'))
print(msg.replace('pen','haha',1))
#结果
i have a haha , i have two haha
i have a haha , i have two pen

判断字符串是否为数字isdigit

msg='i have a pen , i have two pen'
age='18'
print(msg.isdigit())
print(age.isdigit())
#结果
False
True
#可以用来判断用户输入的内容

# print('abc你'.isalpha()) # 字符串中包含的是字母或者中文字符

# 字符串中包含的是字母(中文字符)或数字
# print('ab'.isalnum())
# print('123123'.isalnum())
# print('ab123'.isalnum())

find,rfind,index,rindex,count

msg='hello, this is a test,dhc is a man,test'
print(msg.find('test')) 
print(msg.find('test',0,6))
print(msg.rfind('test'))
print(msg.index('test'))
print(msg.count('test'))

#结果
17   #如果有的花,第一个在什么位置
-1   #没有的话,报-1
35   #如果有的话,从右边第一个的位置
17   #index如果存在的情况下,和find一样,但是没有的话就报错
2    #统计一共出现几处

center,ljust,rjust,zfill

msg='hello, this is a test,dhc is a man,test'
print(msg.center(50,'*'))
print(msg.ljust(50,'*'))
print(msg.rjust(50,'*'))
print(msg.zfill(50))
#格式化填充
#结果
*****hello, this is a test,dhc is a man,test******
hello, this is a test,dhc is a man,test***********
***********hello, this is a test,dhc is a man,test
00000000000hello, this is a test,dhc is a man,test

python入门二: 字符串1

原文:https://www.cnblogs.com/dinghc/p/12213588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!