2022-01-16

how to replace uppercase and lowercase letters in words alternately in a sentence? Python

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.



from Recent Questions - Stack Overflow https://ift.tt/323uICz
https://ift.tt/eA8V8J

No comments:

Post a Comment