[AIGC] python遍历以及字符串的切片
    		       		warning:
    		            这篇文章距离上次修改已过419天,其中的内容可能已经有所变动。
    		        
        		                
                
# 遍历字符串的每个字符
text = "Hello, World!"
for char in text:
    print(char)
 
# 使用切片遍历字符串的子串
# 从索引1开始,直到索引4(不包括4)
print(text[1:4])  # 输出: ello
 
# 使用切片遍历字符串的子串
# 从索引6开始,直到末尾
print(text[6:])  # 输出: World!
 
# 使用切片遍历字符串的子串
# 从开头直到索引5(不包括5)
print(text[:5])  # 输出: Hello
 
# 使用步长为2遍历字符串
print(text[::2])  # 输出: Hlo!
评论已关闭