编写一段python程序,输入了一串字符串,请将其中的数字输出,若没有输入数字,则打印“无数字!”
时间: 2023-06-05 08:08:49 浏览: 38
好的,这是一个纯技术问题,我可以回答。以下是 Python 代码:
```python
# 输入字符串
s = input("请输入一个字符串:")
# 找到字符串中的数字,放到列表中
numbers = []
for char in s:
if char.isdigit():
numbers.append(char)
# 判断是否找到了数字
if len(numbers) > 0:
print("找到的数字为:", end='')
# 输出数字
for num in numbers:
print(num, end='')
else:
print("无数字!")
```
希望这个程序可以帮助您!
相关问题
用Python编写一段程序要求先输入一串字符然后分别统计出其中英文字母空格数字和其他字符的个数并输出结果
可以使用Python中的字符串方法和循环来实现这个程序。代码如下:
```python
string = input("请输入一串字符:")
letters = 0
digits = 0
spaces = 0
others = 0
for char in string:
if char.isalpha():
letters += 1
elif char.isdigit():
digits += 1
elif char.isspace():
spaces += 1
else:
others += 1
print("字母个数:", letters)
print("数字个数:", digits)
print("空格个数:", spaces)
print("其他字符个数:", others)
```
这段程序首先让用户输入一串字符,然后使用循环遍历字符串中的每一个字符,根据其类型累加相应的计数器。最后输出各种类型字符的个数。
编写程序输入一个字符串,删除其中的数字字符
这是一道计算机题目,其大意为:
将程序输入一个字符串,然后从其中删除所有的数字字符。
要回答这道题目,可以使用如下代码:
```python
s = input() # 输入字符串
s_without_digits = ''.join(c for c in s if not c.isdigit()) # 删除数字字符
print(s_without_digits) # 输出结果
```
这段代码的基本思路是:遍历字符串中的每个字符,如果这个字符不是数字字符,就将其加入一个新的字符串中;最后输出这个新字符串,即为去掉数字字符后的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)