python 在“”内引用字符串遍历
时间: 2023-09-14 17:02:55 浏览: 31
可以使用 for 循环遍历字符串中的每一个字符,例如:
```
str = "Hello, World!"
for char in str:
print(char)
```
输出结果为:
```
H
e
l
l
o
,
W
o
r
l
d
!
```
相关问题
python for循环遍历字符串数组
当我们需要遍历一个字符串数组时,可以使用Python的for循环结构来实现。下面是一个示例代码:
```python
str_array = ["hello", "world", "python"]
for str in str_array:
print(str)
```
在上述代码中,我们定义了一个字符串数组`str_array`,然后使用for循环遍历该数组。在每次循环中,将数组中的元素赋值给变量`str`,然后通过`print()`函数打印出来。
输出结果为:
```
hello
world
python
```
这样就完成了对字符串数组的遍历。你可以根据实际需求在循环体内进行其他操作。
python字符串如何遍历
Python中字符串可以使用for循环遍历,用法类似于列表,示例如下:
```python
s = 'hello'
for c in s:
print(c)
```
输出结果为:
```
h
e
l
l
o
```
也可以使用索引遍历字符串,示例如下:
```python
s = 'hello'
for i in range(len(s)):
print(s[i])
```
输出结果也为:
```
h
e
l
l
o
```