用python写一段代码用于接收输入的字符串,并将其两侧的大写字母与数字和空格都去掉,再将剩余字符串中的小写字母改写为大写字母
时间: 2024-03-05 08:53:31 浏览: 22
好的,下面是代码:
```python
string = input("请输入字符串:")
# 去除字符串两侧的大写字母、数字和空格
string = string.strip()
string = ''.join(filter(lambda x: not x.isdigit() and not x.isupper() and not x.isspace(), string))
# 将字符串中的小写字母改写为大写字母
string = string.upper()
print("处理后的字符串为:", string)
```
这段代码会先接收用户输入的字符串,然后使用 `strip()` 函数去除字符串两侧的空格,再使用 `filter()` 函数过滤掉大写字母和数字,得到只包含小写字母的字符串。最后使用 `upper()` 函数将所有小写字母改写为大写字母,并输出处理后的字符串。
相关问题
用python编写程序,接收用户输入的一串字符串,然后将两侧大写字母与数字与空格都去掉,然后将剩余字符串中的小写字母改成大写字母,其余不变,再将修改后字符串输出
好的,我可以帮你写一个Python程序来完成这个任务。下面是代码:
```python
user_input = input("请输入字符串:") # 接收用户输入的字符串
# 去掉字符串两侧的大写字母、数字和空格
str_strip = user_input.strip().strip("0123456789").strip().strip("ABCDEFGHIJKLMNOPQRSTUVWXYZ").strip()
# 将剩余字符串中的小写字母改成大写字母
str_upper = str_strip.upper()
print("修改后的字符串为:", str_upper) # 输出修改后的字符串
```
在这段代码中,我们首先使用 `input()` 函数接收用户输入的字符串,然后使用 `strip()` 函数去掉字符串两侧的空格。接着,我们使用 `strip()` 函数去掉字符串两侧的大写字母和数字,这是通过传入一个包含 0-9 和 A-Z 的字符串作为参数实现的。最后,我们使用 `upper()` 函数将剩余字符串中的小写字母改成大写字母,然后输出修改后的字符串。
希望这个程序可以帮到你!
Python编写一个程序,接收用户通过键盘输入一行字符串(含空格逗号等特殊字 符),然后将其两侧的大写字母与数字与空格都去掉,然后将剩余字符串中的小 写字母改写为大写字母,其余不变,再将修改后的字符串输出。
好的,我可以为你编写这样的程序。你可以参考以下代码:
```python
user_input = input("请输入一行字符串:")
# 去掉两侧的大写字母、数字和空格
trimmed = user_input.strip().strip("0123456789").strip().strip("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# 将小写字母改为大写字母
modified = trimmed.upper()
# 输出修改后的字符串
print(modified)
```
这个程序首先使用 `input` 函数接收用户输入的一行字符串,并使用 `strip` 方法去掉字符串两侧的空格和数字。然后我们用 `strip` 方法再去掉两侧的大写字母,这里使用字符串常量 `"ABCDEFGHIJKLMNOPQRSTUVWXYZ"` 表示所有大写字母。接着我们使用 `upper` 方法将剩余字符串中的小写字母改为大写字母。最后使用 `print` 函数输出修改后的字符串。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)