简体   繁体   中英

how to add a dot before each letter in a string in python

we get a string from user and want to lowercase it and remove vowels and add a '.' before each letter of it. for example we get 'aBAcAba' and change it to '.bcb' . two early things are done but i want some help with third one.

str = input()
str=str.lower()
for i in range(0,len(str)):
    str=str.replace('a','')
    str=str.replace('e','')
    str=str.replace('o','')
    str=str.replace('i','')
    str=str.replace('u','')
print(str)
for j in range(0,len(str)):
    str=str.replace(str[j],('.'+str[j]))
print(str)

A few things:

  • You should avoid the variable name str because this is used by a builtin, so I've changed it to st

  • In the first part, no loop is necessary; replace will replace all occurrences of a substring

  • For the last part, it is probably easiest to loop through the string and build up a new string. Limiting this answer to basic syntax, a simple for loop will work.

st = input()
st=st.lower()
st=st.replace('a','')
st=st.replace('e','')
st=st.replace('o','')
st=st.replace('i','')
st=st.replace('u','')
print(st)
st_new = ''
for c in st:
    st_new += '.' + c
print(st_new)

Another potential improvement: for the second part, you can also write a loop (instead of your five separate replace lines):

for c in 'aeiou':
    st = st.replace(c, '')

Other possibilities using more advanced techniques:

  • For the second part, a regular expression could be used:

    • st = re.sub('[aeiou]', '', st)
  • For the third part, a generator expression could be used:

    • st_new = ''.join(f'.{c}' for c in st)

You can use str.join() to place some character in between all the existing characters, and then you can use string concatenation to place it again at the end:

# st = 'bcb'
st = '.' + '.'.join(st)
# '.b.c.b'

As a sidenote, please don't use str as a variable name . It's the name of the "string" datatype, and if you make a variable named it then you can't properly work with other strings any more. string , st , s , etc. are fine, as they're not the reserved keyword str .

z = "aBAcAba"

z = z.lower() 
newstring = ''
for i in z:
  if not i in 'aeiou':
    newstring+='.'
    newstring+=i  


print(newstring)

Here I have gone step by step, first converting the string to lowercase, then checking if the word is not vowel, then add a dot to our final string then add the word to our final string.

You could try splitting the string into an array and then build a new string with the indexes of the array appending an "." not too efficient but will work.

This does everything.

import re

data    = 'KujhKyjiubBMNBHJGJhbvgqsauijuetystareFGcvb'

matches = re.compile('[^aeiou]', re.I).finditer(data)
final   = f".{'.'.join([m.group().lower() for m in matches])}"

print(final)

#.k.j.h.k.y.j.b.b.m.n.b.h.j.g.j.h.b.v.g.q.s.j.t.y.s.t.r.f.g.c.v.b

thanks to all of you especially allani. the bellow code worked.

st = input()
st=st.lower()
st=st.replace('a','')
st=st.replace('e','')
st=st.replace('o','')
st=st.replace('i','')
st=st.replace('u','')
print(st)
st_new = ''
for c in st:
    st_new += '.' + c
print(st_new)
s = input()
s = s.lower()
for i in s:
    for x in ['a','e','i','o','u']:
        if i == x:
            s = s.replace(i,'')
new_s = ''
for i in s:
    new_s += '.'+ i
print(new_s)

def add_dots(n): return ".".join(n) print(add_dots("test"))

def remove_dots(a): return a.replace(".", "") print(remove_dots("test"))

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