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

Time:01-11

i am new to python programming. I'm trying to learn about arrays and lists.

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

I've tried repeating several times, but I think 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 actually 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. But I don't know how. please can someone advise where? Thanks in advance

Any help would be appreciated.

CodePudding user response:

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(str(result))

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

CodePudding user response:

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 

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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
  •  Tags:  
  • Related