python编写程序,读入一行文本,文本是一个长度不超过255的英文句子,单词之间有一个或一个以 上的空格,输出: ①统计单词的个数; ②一个对应的英文句子,其中原句中的所有小写字母均转换成大写字母,大写字母转换成小写字母; ③删除所有空格符后对应的句子。
时间: 2023-02-17 12:25:32 浏览: 115
```python
sentence = input("请输入一个长度不超过255的英文句子:")
# 统计单词个数
word_count = len(sentence.split())
print("单词个数为:", word_count)
# 小写字母转换成大写字母,大写字母转换成小写字母
transformed_sentence = sentence.swapcase()
print("转换后的句子为:", transformed_sentence)
# 删除所有空格符后的句子
no_space_sentence = sentence.replace(" ", "")
print("删除空格后的句子为:", no_space_sentence)
```
这是一个读入一行文本,统计单词个数,转换小写字母为大写字母,大写字母为小写字母,删除所有空格符后对应的句子的python程序。使用input()函数读入一个字符串,使用split()函数将字符串分割为单词,使用len()函数计算单词个数。使用swapcase()函数将小写字母转换成大写字母,大写字母转换成小写字母。使用replace()函数删除所有空格符后对应的句子。
相关问题
读入一行英文,单词之间以一个空格隔开,如果句中有标点符号Python
代码实现如下:
```python
import string
# 读入一行英文
text = input("请输入一行英文:")
# 去除标点符号
text = text.translate(str.maketrans("", "", string.punctuation))
# 将字符串按空格分割成单词列表
words = text.split()
# 输出单词列表
print(words)
```
运行示例:
```
请输入一行英文:Hello, World! This is a test.
['Hello', 'World', 'This', 'is', 'a', 'test']
```
python编写程序,把一个英文句子中的单词倒置,标点符号不倒置
可以使用字符串的replace()方法来实现。假设英文句子为sentence,需要替换的单词为word,代码如下:
sentence = 'This is a test sentence.'
word = 'test'
new_sentence = sentence.replace(word, '.'*len(word))
print(new_sentence)
输出结果为:
This is a ..... sentence.