2

I am new to Python programming. I'm trying to learn about lists.

I want to create a new program that can convert each word into uppercase and lowercase letters alternately in a sentence.

I’m stuck here:

input_text = "hello my name is rahul and i'm from india"
result = ""
myArr = input_text.split(' ')

for idx in range(len(myArr)):
  if idx % 2 == 0 :
    result = result + myArr[idx].upper()
  else:
    result = result + myArr[idx].lower()

print(str(result))

With this code I can get, for example:

input : "hello my name is rahul and i'm from india"
output : "HELLOmyNAMEisRAHULandI'MfromINDIA"

but what I am trying to get is:

input : "hello my name is rahul and i'm from india"
output : "HELLO my NAME is RAHUL and I'M from INDIA"

I want to add a space to each word in the sentence.

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33

6 Answers6

4

You've sort of got it -- the best way to do this is to add the capitalized/lowercased words to a list, then use .join() to turn the word list into a string with each word separated by a space:

input_text = "hello my name is rahul and i'm from india"
result = ""
myArr = input_text.split(' ')
words = []
for idx in range(len(myArr)):
  if idx % 2 == 0 :
    words.append(myArr[idx].upper())
  else:
    words.append(myArr[idx].lower())

result = ' '.join(words)
print(result)

Contrary to what other answerers have suggested, using repeated concatenation is a bad idea for efficiency reasons.

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
3

Using a list comprehension we can try:

input_text = "hello my name is rahul and i'm from india"
words = input_text.split()
output = ' '.join([x.upper() if ind % 2 == 0 else x for ind, x in enumerate(words)])
print(output)  # HELLO my NAME is RAHUL and I'M from INDIA
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

You can add all the values to a list and finally join them using the str.join method.

input_text = "hello my name is rahul and i'm from india"
result = []
myArr = input_text.split(' ')

for idx in range(len(myArr)):
    if idx % 2 == 0:
        res = myArr[idx].upper()
    else:
        res = myArr[idx].lower()
    result.append(res)

print(" ".join(result))

Output:

HELLO my NAME is RAHUL and I'M from INDIA
pppig
  • 1,215
  • 1
  • 6
  • 12
2

In addition to the other answers, I provided another type of answers here using enumerate (see https://www.geeksforgeeks.org/enumerate-in-python/), which loop a list (or tuple) with its index:

input_text = "hello my name is rahul and i'm from india"

result = []
for idx, word in enumerate(input_text.split()):
    result.append(word.upper() if idx % 2 == 0 else word.lower())
print(' '.join(result))

The answer also can be shorter like this using List Comprehension (see if/else in a list comprehension)

input_text = "hello my name is rahul and i'm from india"

result = [word.upper() if idx % 2 == 0 else word.lower() for idx, word in enumerate(input_text.split())]
print(' '.join(result))

It would be helpful to know for your Python life :)

Park
  • 2,446
  • 1
  • 16
  • 25
1

I have added space where you are appending to the list:

input_text = "hello my name is rahul and i'm from india"
result = ""
myArr = input_text.split(' ')

for idx in range(len(myArr)):
    if idx % 2 == 0 :
        result = result + myArr[idx].upper() + " "  # added a space
    else:
        result = result + myArr[idx].lower() + " "  # added a space

print(str(result))

This gives:

HELLO my NAME is RAHUL and I'M from INDIA 
Sibtain Reza
  • 513
  • 2
  • 14
1

You can add a space to the result after adding each word, i.e.

result = result + ' '

This will also add a space at the end of the string, which may not be desirable. You can remove it with the rstrip() function:

result = result.rstrip()

By the way, result is already a string, so you don't need to cast it to a string for the print statement.

So, put it all together:

input_text = "hello my name is rahul and i'm from india"
result = ""
myArr = input_text.split(' ')

for idx in range(len(myArr)):
  if idx % 2 == 0 :
    result = result + myArr[idx].upper()
  else:
    result = result + myArr[idx].lower()
  result = result + ' '

result = result.rstrip()    

print(result)
Marion
  • 198
  • 6