写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度
时间: 2023-06-05 07:48:02 浏览: 91
好的,以下是一段可以求字符串长度的函数示例:
```python
def get_string_length(input_str):
length = 0
for i in input_str:
length += 1
return length
```
使用该函数可以获取一个字符串的长度,例如:
```python
my_str = "Hello, world!"
length = get_string_length(my_str)
print("字符串长度为:", length)
```
输出结果为:
```
字符串长度为: 13
```
希望这个示例能帮助您!
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)