[英]How add "." before each letter in string
as title says i simply need to add a .正如标题所说,我只需要添加一个 . before each letter in my string while having vowels removed and making it lowercase i got it working just cant add the .s there here is my code在我的字符串中的每个字母之前,同时删除元音并将其设为小写,我得到了它的工作,只是无法添加 .s 这里是我的代码
s = str(input())
vowels = ('a','e','o','u','i','A','E','O','U','I')
for letter in s:
if letter in vowels:
s = s.replace(letter,'').replace()
print(s)
Use:用:
s = input()
vowels = set('aeoui')
print(''.join([f'.{x}' for x in s.lower() if x not in vowels]))
Sample run :示例运行:
Hello
.h.l.l
All other answers will insert a .
所有其他答案将插入一个.
in front of every character in the string, but you specified that you want letters only.在字符串中的每个字符前面,但您指定只需要字母。 So I am assuming that you only want az
to be prepended with a .
所以我假设你只希望az
在前面加上.
for which I suggest re.sub
:为此,我建议re.sub
:
import re
s = "This is some test string. It contains some symbols also ()!!"
result = re.sub('[aeoui]', '', s.lower()) # remove vowels and make lowercase
result = re.sub("([a-z])", r".\1", result) # prepend '.' to every letter
print(result)
Outputs:输出:
.t.h.s .s .s.m .t.s.t .s.t.r.n.g. .t .c.n.t.n.s .s.m .s.y.m.b.l.s .l.s ()!!
You can do it step by step:你可以一步一步来:
Replace all the vowels in the string with ''将字符串中的所有元音替换为 ''
for i in s:
for j in vowels:
s=s.replace(j,'')
Convert the string into lowercase:将字符串转换为小写:
s=s.lower()
Adding '.'添加“.” in between each letters:在每个字母之间:
s='.' + '.'.join(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.