切片
name = "Python Java C++"
print(name[0])
print(name[-1])
print(name[0:6])
print(name[:6])
print(name[1:])
print(name[-3:])
print(name[:8])
print(name[:])
print(name[::1])
print(name[:6:2])
print(name[::2])
print(name[::-1])
print([i for i in range(10)])
print([i for i in range(10)][::-1])
print([i for i in range(9, -1, -1)])
print("1", name[3::-1])
print("2", name[3::1])
print("3", name[-3::1])
print("4", name[-3::-1])
print("4", name[-3:-1:-1])
print("4", name[-3:-1:1])
ls = []
for num in range(1, 1000):
if num == int(str(num)[::-1]):
ls.append(num)
print(ls)
字符串
name = " Python Java C++ "
print(len(name))
print(type(name))
print(len(name.strip()))
name = name.strip()
print(len(name))
print(name.split())
path = "/Users/ericli/opt/anaconda3/python.app/Contents/MacOS/python /Users/ericli/PycharmProjects/luliyu_python/day06/demo26.py"
print(path.split("/"))
print(name.index("Py"))
print(name.index("+"))
print(name.index("+", 14))
print("--"*10)
print(name.find("Py"))
print(name.find("+"))
print(name.find("+", 14))
print(name.find("+", 3, 9))
def find(ori, sub, start = None, end = None):
if start is None:
start = 0
if end is None:
end = len(ori)
for i in range(start, end):
if ori[i] == sub:
return i
return -1
print(find(name, "+"))
print(find(name, "+", 3, 5))
ls = ['Python', 'Java', 'C++']
print(" ".join(ls))
print("".join(ls))
print("/".join(ls))
print("_".join(ls))
from random import randint
prices = [f"${randint(300, 1000)}" for _ in range(10)]
print(prices)
sums = 0
for v in prices:
v = int(v.replace("$", ""))
sums += v
print(sums/len(prices))
text = "When you When you When, you When you face, the world alone, "
text = text.replace(",", "")
print(text)
ts = text.split()
print(ts)
counts = {}
for word in ts:
counts[word] = counts.get(word, 0) + 1
print(counts)
|