Python Forum

Full Version: Python Exercise Doubt
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I got a question for you guys, I've been dealing with this Python exercise since this morning but yeah! I'm completely stuck so I came here to get some advice since I don't have any clue about how to get this exercise done!

I need to print out only every second character of the string. Can someone teach me how to code this exercise?

Thank you, guys!
Can you post what you have tried?
This is what I tried!

line = input('Please introduce a string: ')
result = line[0]
for i in range(2, len(line), 2):
result += ' ' + line[i]
print(result)

I need to print out "Hello World"

The result must be like the example shown below:

Every second character

Hello World -- > Input

el ol --> Result

(Apr-21-2020, 12:37 PM)anbu23 Wrote: [ -> ]Can you post what you have tried?

This is what I tried!

line = input('Please introduce a string: ')
result = line[0]
for i in range(2, len(line), 2):
result += ' ' + line[i]
print(result)

I need to print out "Hello World"

The result must be like the example shown below:

Every second character

Hello World -- > Input

el ol --> Expected Result
line = input('Please introduce a string: ')
result = ""
for i in range(1, len(line), 2):
 result += ' ' + line[i]
print(result)
(Apr-21-2020, 01:02 PM)anbu23 Wrote: [ -> ]
line = input('Please introduce a string: ')
result = ""
for i in range(1, len(line), 2):
 result += ' ' + line[i]
print(result)

It works perfectly!

Thank you so much for your help!