Python 字符串——巧取值和列表——巧取值 对比
1.字符串取值实例:
samp_string = "Whatever you are, be a good one." for i in samp_string:
print(i)
for i in range(,len(samp_string)-,):
print(samp_string[i]+samp_string[i+]) print('A=',ord("A"))
print('65=',chr()) print('桃:',ord("桃"))
print('',chr()) # # You can get a character by referencing an index
# print(samp_string[]) print(samp_string[])
# # Get the last character
print(samp_string[-])
#
# # 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[:])
#
# # Get everything starting at an index
print(samp_string[:])
# print()
#
print(samp_string)
print(samp_string[::])
print(samp_string[::])
#
# # Reverse the string
print(samp_string[::-])
# # Palindrome
print('I did, did I?'[::-])
print('No lemon, no melon'[::-])
#
# # Practical use
url = "http://pythonabc.org"
# Get the top level domain
print(url[-:])
# Print the url without the http://
print(url[:])
# Print the url without the http:// or the top level domain
print(url[:-])
2,列表取值取值实例
my_list=[, , , , , , , , , ]
# , , , , , , , , ,
# -,-,-,-,-,-,-,-,-,- # 取其中一个值
print(my_list[])
print(my_list[-])
print(my_list[-]) # 取其中几个值
print(my_list[:])
print(my_list[-:-])
print(my_list[-:-:]) # 逆序输出
print(my_list[::-])
print(my_list[-:-:-]) # 注意逆序输出,开始点在结束点的后面
print(my_list[-:-:-]) # 注意逆序输出,开始点在结束点的后面 strList=["string",,21.0]
# 列表基本操作
print("列表长度:",len(strList))
print("列表输出",strList)
print("两个列表相加为一个列表:",my_list+strList) # 判断一个元素是否为列表中的元素
print("string" in strList)
# ************列表相关方法*************************
# 找出特定元素的索引,字符串使用find方法找到,列表用index方法找到
print(strList.index())
for element in strList:
print("{}:{}".format(strList.index(element),element))
# 特定元素的的个数 字符串和数组都是使用count方法计算特定子字符串和元素的个数
print("string 的个数:",strList.count('string')) # 添加元素
strList.append("final")
print("string 的更新后个数:",len(strList))