编自己编写一个程序,键盘任意输入一个字符串,字符串以#号结束,然后将该字符串逆序输出,请不要调用系统函数
时间: 2024-03-01 14:55:00 浏览: 81
以下是 Python 代码实现:
```python
s = input("请输入字符串:")
result = ""
for i in range(len(s)-1, -1, -1):
result += s[i]
print("逆序输出结果:{}".format(result))
```
程序首先通过 `input()` 函数获取用户输入的字符串,然后使用一个循环遍历字符串中的每个字符,并逆序将它们拼接到一个新的字符串变量 `result` 中。最后输出 `result` 即可得到逆序输出的结果。
相关问题
python编写程序,用户输入一个字符串,以回车结束,统计其中英文字母、数字字符和其他字符的个数(回车符代表结束,不计入统计)。
请看下面的代码实现:
```python
s = input()
letters = 0
digits = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isdigit():
digits += 1
else:
others += 1
print("字母:{},数字:{},其他字符:{}".format(letters, digits, others))
```
代码解释:
1. 首先让用户输入一个字符串,使用 `input()` 函数实现。
2. 然后用 `for` 循环遍历字符串中的每个字符,判断字符的类型,分别统计字母、数字和其他字符的个数。
3. 最后使用 `print()` 函数输出结果,其中使用字符串的 `format()` 方法格式化输出字符串。
这道题要求编写一个python程序,输入字符串,如: "I use Python!" ,之后能统计其中每个字母出现的频率并且显示所有出现 1 次以上的字母。
可以使用Python中的字典来统计每个字母出现的频率,代码如下:
```
s = input("请输入字符串:")
d = {}
for c in s:
if c.isalpha():
if c in d:
d[c] += 1
else:
d[c] = 1
for k in d:
if d[k] > 1:
print(k, end=' ')
```
运行结果如下:
```
请输入字符串:I use Python!
I u s e P y t h o n
```
阅读全文
相关推荐














