简体   繁体   中英

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 = 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 . for which I suggest 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM