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(65))

print(‘桃:‘,ord("桃"))
print(‘26690‘,chr(26690))

# # You can get a character by referencing an index
# print(samp_string[0])

print(samp_string[0])
# # Get the last character
print(samp_string[-2])
#
# # Get the string length
print("Length : ", len(samp_string))
#
# # Get a slice by saying where to start and end
# # The 4th index isn‘t returned
print(samp_string[0:4])
#
# # Get everything starting at an index
print(samp_string[8:])
# print()
#
print(samp_string)
print(samp_string[::])
print(samp_string[::2])
#
# # Reverse the string
print(samp_string[::-1])
# # Palindrome
print(‘I did, did I?‘[::-1])
print(‘No lemon, no melon‘[::-1])
#
# # Practical use
url = "http://pythonabc.org"
# Get the top level domain
print(url[-3:])
# Print the url without the http://
print(url[7:])
# Print the url without the http:// or the top level domain
print(url[7:-4])

2,列表取值取值实例

my_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#        0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#       -10,-9,-8,-7,-6,-5,-4,-3,-2,-1

# 取其中一个值
print(my_list[0])
print(my_list[-10])
print(my_list[-1])

# 取其中几个值
print(my_list[0:5])
print(my_list[-10:-5])
print(my_list[-10:-5:2])

# 逆序输出
print(my_list[::-1])
print(my_list[-1:-10:-1]) # 注意逆序输出,开始点在结束点的后面
print(my_list[-1:-10:-2]) # 注意逆序输出,开始点在结束点的后面

strList=["string",520,21.0]
# 列表基本操作
print("列表长度:",len(strList))
print("列表输出",strList)
print("两个列表相加为一个列表:",my_list+strList)

# 判断一个元素是否为列表中的元素
print("string" in strList)
#  ************列表相关方法*************************8
# 找出特定元素的索引,字符串使用find方法找到,列表用index方法找到
print(strList.index(520))
for element in strList:
    print("{}:{}".format(strList.index(element),element))
# 特定元素的的个数   字符串和数组都是使用count方法计算特定子字符串和元素的个数
print("string 的个数:",strList.count(‘string‘))

# 添加元素
strList.append("final")
print("string 的更新后个数:",len(strList))

原文地址:https://www.cnblogs.com/lyxcode/p/10914336.html

时间: 2024-07-30 03:10:32

Python 字符串——巧取值和列表——巧取值 对比的相关文章

nodejs抓取数据一(列表抓取)

纯属初学...有很多需要改进的地方,请多多指点... 目标是抓取58同城 这个大分类下的列表数据: http://cd.58.com/caishui/?PGTID=14397169455980.9244072034489363&ClickID=1 简单分析: 1. 按照以下二级分类来获取每个列表的数据, 2. 主要分页: 可以看出,其分页是pn5 这里设置的,那么这个5就是页码了. http://cd.58.com/dailijizh/pn5/?PGTID=1177429071887065549

python 字符串方法及列表,元组,字典(一)

字符串 str 注: 若想要保持单引号和双引号为字符串的一部分 1)单双引号交替使用, 2)使用转义字符\ 3)成对三个引号被存在变量里 二.字符串详细用法 字符串的单个取值例 p_1=”hello” 字符串元素定位置,通过索引 正序(→)  反序(←)开始值从0开始,反序从-1开始 取值方式  字符串名[索引值] 例print(p_1[-1])  取o ①字符串切片:字符串名[m:n:k]m:索引起始位置,n:索引结束位置+1,k:步长 默认值为1,取左不取右 例 1,print(p_1[2:

python -- 字符串、列表、元组、字典操作

1. 字符串 字符串是不可变变量,不能通过下标来修改它的值,但可以通过以下方法取值: 1 name = "123" #定义字符串 2 print(name[0]) #通过下标取值 字符串也可通过for循环来循环对象里的每一个元素. 常用的字符串方法 所有字符串的方法,都不会修改原字符串的值,都会产生一个新的字符串. 1 import string #导入string 2 print(string.ascii_letters+string.digits) #大小写字母+数字 3 4 s.

python字符串、list列表、字典、文件的相关操作

1.布尔类型 字符串 常用的操作方法 .count() .find() .join() .encode() .decode()#这个是bytes类型才有的 .endswith('.txt')#字符串是否以xx结尾 startswith('A')#字符串是否以xx开头 .isdigit() .strip() .replac('xx','') .split(',')#以xx来分割字符串,返回的是一个list .splitlines()#以换行符来分割字符串,返回的是一个list 2.list 列表

Python -- 字符串 列表 元组 字典

小Q浪花有意千重雪桃李无言一队春一壶酒一竿纶世上如侬有几人.  ---李煜<渔歌子> --------------------------------------------------------------------------------------- 序列  是Python中最基本的数据结构.序列中每一个元素的位置都有其对应数字编码或索引比如第一个元素的编码是0第二个元素的索引是1............. 序列中可进行的操作索引.切片.加.乘.检查成员另外的长度.最大最小值等内建函

Python 字符串、列表、元组、索引、切片

一.简要概述 what is list? 1.用[ 和 ]括起来,用逗号间隔每个数据项 2.数据项可是同类型数据也可以是不同类型数据(数字.字符串.浮点型) 3.list里面可以有list作为其数据项 4.数据项对应的位置为索引编号(index).默认第一个是0 5.有序的数据集合 what is string? 1.用单引号.双引号.三引号引起来. 2.字符串是一个常量不可被修改,它的主要用途就是读其元素. what is tuple? 1.元组和字符串均是不可被修改的.但是访问的方式都是一样

python字符串、列表、元组

字符串的常用方法: name.count('h')统计h在name中出现的次数 name.find('h')查找h的索引 '?'.join(name)使用问好拼接 name.encode('gb2312')将字符串转为gb2312编码 name.decode('gb2312'),表示将gb2312编码的字符串转换成unicode编码 name.endswith('.txt')字符串是否以xx结尾 布尔型 startswith('A')字符串是否以xx开头 布尔型 name..isdigit()判

Python字符串、列表、元组、集合、字典方法

列表list 1.L.append(object) -> None 在列表末尾添加单个元素,任何类型都可以,包括列表或元组等 2.L.extend(iterable) -> None 以序列的形式,在列表末尾添加多个元素 3.L.insert(index, object) -> None 在index位置处添加一个元素 4.L.clear() -> None 清除列表所有元素,成为空列表 5.L.copy() -> list 获得一个列表副本 6.L.count(A) -&g

python字符串和List:索引值以 0 为开始值,-1 为从末尾的开始位置;值和位置的区别哦

String(字符串)Python中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符. 字符串的截取的语法格式如下: 变量[头下标:尾下标]索引值以 0 为开始值,-1 为从末尾的开始位置.[一个是值,一个是位置.看图.所以str = 'Runoob' print (str[0:-1]) # 输出第一个到倒数第二个的所有字符没问题] 加号 + 是字符串的连接符, 星号 * 表示复制当前字符串,紧跟的数字为复制的次数.实例如下: 实例#!/usr/bin/python3