关于python里标点符号的处理和首字母大小写
答案:1 悬赏:10
解决时间 2021-02-21 17:12
- 提问者网友:若相守£卟离
- 2021-02-21 12:52
关于python里标点符号的处理和首字母大小写python初学者,最近在写pig latin。对句子翻译,要求翻译后原来大写字母位置的字母也要大写,其他都是小写,比如“My name is Mike”,翻译后是“Ymay amenay isyay Ikemay”。还要标点符号不能变,这个也不会,想问如何变字符串的时候只考虑字母,标点不考虑进去,比如说“hello,”,翻译出来要是“ellohay,”,而不是“ello,hay”
最佳答案
- 二级知识专家网友:我叫很个性
- 2021-02-21 13:00
词法分析器首先是断句,可以考虑用正则表达式,下面的例子可以参考
import re
s = "hello, "
s = "My name is Mike."
s = "I'm ab!"
while s != '':
match = re.search('^[a-z|A-Z|\']+', s)
if match:
print('<' + s[:match.end()] + '>')
s = s[match.end():]
else:
print('<' + s[:1] + '>')
s = s[1:]
import re
s = "hello, "
s = "My name is Mike."
s = "I'm ab!"
while s != '':
match = re.search('^[a-z|A-Z|\']+', s)
if match:
print('<' + s[:match.end()] + '>')
s = s[match.end():]
else:
print('<' + s[:1] + '>')
s = s[1:]
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯