Python字符窜取值

# coding:utf-8

msg = "hello world *te*st *?/ * /   -*2ego2*n , -*= 123"
#按索引取值
print(msg[0],type(msg))#取第一个值
print(msg[-1],type(msg))#取最后一个值
#按切片取值
print(msg[1:4],type(msg))#取固定范围值,顾头不顾尾;左闭右开;从前面取值; 从左往右,正向
print(msg[-3:-1],type(msg))#取固定范围值,顾头不顾尾;左闭右开;从后面取值; 从左往右,正向
print(msg[-1:-3:-1],type(msg))# 从右往左,反向取值必须指定步长(反向取值步长也是负数)
print(msg[6:13:2],type(msg))# wld*,在固定范围中 每有两个取其中前一个,指定步长为2
print(msg[6:],type(msg))# 取固定范围值,指定开头到结尾,结尾不用写,反之一样

print(len(msg))# 取字符串长度

#成员运算,判断一个子字符串是否在另一个字符窜中,使用in或not in
response_msg = "this is a boy"
print(‘boy‘ in response_msg)#返回True,判断成功

# 移除空白 strip
msg = " * **/,hello wor*ld**=** "
print(msg.strip())#strip不指定数值,默认去除字符窜左右两边的空格
print(msg.strip(‘* ‘))#指定要去除的字符后必须加空格,多个需过滤的字符也以空格分开
print(msg.strip(‘* / , h ‘))# 去除多个字符以空格隔开,(不去除的整体字符窜中的指定字符)

# 切分字符窜split:把一个字符窜按照某种分隔符切成一个列表
info = "username1:user:pwd:00 user:/user:/bin:/path"
res = info.split(‘:‘,maxsplit=-1)#指定按“:”切分,maxsplit指定切分次数,-1为不限制
print(res,type(res))
info2 = "hello test.txt 22"
res = info2.split()#不指定切分字符,则按空格切分
print(res,type(res))
res2 = info2.split(‘ ‘,maxsplit=1)# maxsplit=1,只切分一次
print(res2,type(res2))
#把列表切分为字符窜
res = [‘username1‘, ‘100‘, ‘/path‘]
print(res,type(res))# 字符窜
list_string = ‘:‘.join(res)#把列表切分为字符窜(前提是 列表中所有元素都是字符窜)
print(list_string,type(list_string))

#循环取值
msg = ‘hello world‘
i=0
while i < len(msg):
    print(msg[i])  #依赖索引取值
    i+=1
#for 循环取值
msg = ‘hello world‘
for item in msg:   #不依赖索引取值,常用的取值方式,可以取所有类型的值(如:list、dict)
    print(item)
#for +break
#for +continue

原文地址:https://www.cnblogs.com/xiazhenyu/p/11669156.html

时间: 2024-08-18 14:14:16

Python字符窜取值的相关文章

Python 字符串——巧取值和列表——巧取值 对比

Python 字符串——巧取值和列表——巧取值 对比 1.字符串取值实例: samp_string = "Whatever you are, be a good one." for i in samp_string: print(i) for i in range(0,len(samp_string)-2,2): print(samp_string[i]+samp_string[i+1]) print('A=',ord("A")) print('65=',chr(6

python数组冒号取值操作

1.冒号的用法 1.1 一个冒号 a[i:j] 这里的i指起始位置,默认为0:j是终止位置,默认为len(a),在取出数组中的值时就会从数组下标i(包括)一直取到下标j(不包括j) 在一个冒号的情况下若出现负数则代表倒数某个位置 a[i:-j] 这里就是从下标i取到倒数第j个下标之前(不包括倒数第j个下标位置的元素) 1.2 两个冒号 a[i:j:h] 这里的i,j还是起始位置和终止位置,h是步长,默认为1 若i/j位置上出现负数依然倒数第i/j个下标的位置,h若为负数则是逆序输出,这时要求起始

Python练习-列表取值

【Python学习之旅】---数据类型(数字、字符窜、列表、元组、字典、布尔值)

1.将字符窜转换为数字,即输出数据类型和数字 输出结果: 2.计算一个数字至少用几位二进制数来表示 输入结果: 3.将字符窜用2进制来表示并转换为整数 输出结果: 原文地址:https://www.cnblogs.com/chenyuxia/p/11877118.html

python 字典合并,字典取值,列表转字典

1.字典的合并 # 第一种 用Python的内置方法,dict与**解包的方式 >>> a = {'name':1,'b':2} >>> b = {'name':1, 'c':10} >>> c = dict(a, **b) >>> c {'name': 1, 'b': 2, 'c': 10} # 第二种 用字典的update()方法 # 可以带权重的,是用b中的元素将a中的元素更新,a的元素会发生改变,但是内存地址不会发生改变 &g

c#中Dictionary&lt;object, object&gt;存储网络字符取值问题

roleInfo存储的是网络的map数据,key值roleID长度居然是7,而本地定义的key2="roleID"长度是6,导致tmp[key2]总是取不到值.最后通过string.Compare通过指定长度比对,再通过tmp[key]取值才解决问题.对应为什么key值roleID长度是7,暂时未找到原因.

python 嵌套字典比较值,取值

#取值import types allGuests = {'Alice': {'apples': 5, 'pretzels': {'12':{'beijing':456}}}, 'Bob': {'ham sandwiches': 3, 'apple': 2}, 'Carol': {'cups': 3, 'apple pies': 1}} def dictget(dict1,obj,default=None): for k,v in dict1.items(): if k == obj: prin

python re.findall(rule,data),根据左右边界取值url中参数的值

import re ''' 取值postid,左边界"postid=",右边界"&" ''' url="http://wwww.baidu.com/aspx?postid=6232&actiontip='保存成功'" postid=re.findall(r"postid=(.*?)&",url)[0] print(postid) def findall_data(data,LB=""

关于python的列表操作(一):取值,增加,修改,删除

# 列表操作 name_list = ["wang", "niu", "bai", "sui"] # 取值 print("**********取值***************") name = name_list[0] print(name) # 取索引 print("**********取索引***************") name_index = name_list.index