python 字符串常用操作+yield的用法

1. 字符串find\count\replace

string = "abcdefgab"

print(string.find("ab"))
print(string.find("gg"))
print(string.count("ab"))
print(string.replace("aa", "ab"))
E:\pythonPrj\common\venv\Scripts\python.exe E:/pythonPrj/common/02-string.py
0
-1
2
abcdefgab

Process finished with exit code 0

2. yield用法

def function():
    for i in range(5):
        yield i
        yield i + 1


new_function = [x * x for x in function() if x % 2 == 0]
print(new_function)
for j in new_function:
    print(j, "")
E:\pythonPrj\common\venv\Scripts\python.exe E:/pythonPrj/common/03-yield.py
[0, 4, 4, 16, 16]
0 
4 
4 
16 
16 

Process finished with exit code 0

你可能感兴趣的:(01-python基础知识,python)