2

Here is what I want to do but doesn't work:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = list(mystring)

for c in array:
    if c in toUpper:
        c = c.upper()
print(array) 

"e" and "o" are not uppercase in my array.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
BoumTAC
  • 3,531
  • 6
  • 32
  • 44

9 Answers9

8

You can use the str.translate() method to have Python replace characters by other characters in one step.

Use the string.maketrans() function to map lowercase characters to their uppercase targets:

try:
    # Python 2
    from string import maketrans
except ImportError:
    # Python 3 made maketrans a static method
    maketrans = str.maketrans 

vowels = 'aeiouy'
upper_map = maketrans(vowels, vowels.upper())
mystring.translate(upper_map)

This is the faster and more 'correct' way to replace certain characters in a string; you can always turn the result of mystring.translate() into a list but I strongly suspect you wanted to end up with a string in the first place.

Demo:

>>> try:
...     # Python 2
...     from string import maketrans
... except ImportError:
...     # Python 3 made maketrans a static method
...     maketrans = str.maketrans 
... 
>>> vowels = 'aeiouy'
>>> upper_map = maketrans(vowels, vowels.upper())
>>> mystring = "hello world"
>>> mystring.translate(upper_map)
'hEllO wOrld'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @JonClements No wonder AIs are so intelligent today to scrape of codes from puppy's editor – Bhargav Rao Mar 27 '15 at 19:22
  • 1
    @JonClements: guess what I [still had open in the other tab](https://stackoverflow.com/questions/29307814/how-to-replace-characters-in-string-by-the-next-one/29307923#29307923). :-P – Martijn Pieters Mar 27 '15 at 19:24
5

You are not making changes to the original list. You are making changes only to the loop variable c. As a workaround you can try using enumerate.

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = list(mystring)

for i,c in enumerate(array):
    if c in toUpper:
        array[i] = c.upper()

print(array) 

Output

['h', 'E', 'l', 'l', 'O', ' ', 'w', 'O', 'r', 'l', 'd']

Note: If you want hEllO wOrld as the answer, you might as well use join as in ''.join(array)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
3

You can do:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']

>>> ''.join([c.upper() if c in toUpper else c for c in mystring])
hEllO wOrld
  • No reason to generate a list. Omit the brackets and go with a generator expression: `''.join(c.upper() if c in toUpper else c for c in mystring)` – Steven Rumbalski Mar 27 '15 at 19:19
  • 1
    @StevenRumbalski - Actually, in the case of `str.join`, a list comprehension is generally more efficient. See http://stackoverflow.com/a/9061024/2555451. Of course, with small lists, the difference is pretty negligible. –  Mar 27 '15 at 19:33
  • @iCodez: Fair enough. Stylistically, I still prefer the (nearly imperceptably) simpler code. Performance can't be our only concern. We are coding in Python after all. (Performance-wise, I think that the `maketrans` approach probably wins.) – Steven Rumbalski Mar 27 '15 at 20:26
1

Use generator expression like so:

newstring = ''.join(c.upper() if c in toUpper else c for c in mystring)
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
1

The problem is that al c is not used for anything, this is not passing by reference.

I would do so, for beginners:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = []
for c in mystring:
    if c in toUpper:
        c = c.upper()
    array.append(c)
print(''.join(array))
neiesc
  • 633
  • 1
  • 7
  • 16
0

This will do the job. Keep in mind that strings are immutable, so you'll need to do some variation on building new strings to get this to work.

myString = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
newString = reduce(lambda s, l: s.replace(l, l.upper()), toUpper, myString)
esc
  • 26
  • 3
0

Please try this one

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']

array = list(mystring)
new_string = [x.upper() if x in toUpper else x for x in array ]



new_string = ''.join(new_string)
print new_string
J.Jai
  • 597
  • 1
  • 5
  • 9
0

Easy method

name='india is my country and indians are my brothers and sisters'
vowles='a','e','i','o','u'
for name_1 in name:
    if name_1 in vowles:
        b=name_1.upper()
        print(b,end='')
    else:
        print(name_1,end='')
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
SDP
  • 1
-1

Here is code :

    name='india is my country and indians are my brothers and sisters'
    vowles='a','e','i','o','u'
    for name_1 in name:
        if name_1 in vowles:
            b=name_1.upper()
            print(b,end='')
        else:
            print(name_1,end='')
Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
SDP
  • 1