0

Beginner in python here, and I have a code that is supposed to slice a string evenly and oddly and display it.

Here is my code:

def even_bits(str):
    result = ""  
    for i in range(len(str)):
        if i % 2 == 0:
            result = result + str[i]
    return result
def odd_bits(str):
    result = ""  
    for i in range(len(str)):
        if i % 2 == 1:
            result = result + str[i]
    return result


for i in range(int(input())):
    w = input('')    
    print(even_bits(w), ' ' ,odd_bits(w))

This runs correctly however gives output as follows:

Sample Input: 
2
Hello
World

Sample Output: 
2
Hello
Hlo el

World
Wrd ol

How do I format the output such that I get output as follows:

Sample Output:
Hlo el
Wrd ol

Thank You in advance.

2
  • A tip: Don't overwrite builtin methods or keywords. str is a builtin python method that converts an object into a string. Commented May 22, 2019 at 18:58
  • Yep, thank you changed the builtin keyword. Commented May 22, 2019 at 20:17

2 Answers 2

1

You can first declare an input list that contains all input strings. Then iterate over the input list and print the even and odd characters:

def even_bits(chain):
    result = ""
    for i in range(len(chain)):
        if i % 2 == 0:
            result = result + chain[i]
    return result


def odd_bits(chain):
    result = ""
    for i in range(len(chain)):
        if i % 2 == 1:
            result = result + chain[i]
    return result


input_list = [] # input list that contains all input strings

for i in range(int(input())):
    w = input('')
    input_list.append(w)

# iterate over input list to print even and odd characters
for inp in input_list:
    print(even_bits(inp), ' ', odd_bits(inp))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Carlos, this is helpful, I did not think of having a separate list to store all the values and then run it in a loop. Appreciate the insight
0

You can make two result lists. One for the even outputs, one for the odd outputs, then zip them and print each element. Also, you can easily take the even and odd letters using a single slice.

x = "Hello world"
# slicing looks like [START:STOP:STEP], so we can do
evens = x[0::2] # Will start at 0, go to the end, and take every 2nd character
odds  = x[1::2] # Will start at 1, go to the end, and take every 2nd character
print(evens) # >>> Hlowrd
print(odds)  # >>> el ol

This works even if you have an empty string.

Putting it all together, it could look like this:

def even_bits(my_str):
    return my_str[0::2]

def odd_bits(my_str):
    return my_str[1::2]

even_results = []
odd_results = []
for i in range(int(input("How many inputs: "))):
    w = input('Input # {}: '.format(i+1))    
    even_results.append(even_bits(w))
    odd_results.append(odd_bits(w))

for ev, od in zip(even_results, odd_results):
    print(ev, od)

Output:

How many inputs: 2
Input # 1: Hello
Input # 2: World
Hlo el
Wrd ol

1 Comment

Hi @SyntaxVoid, thanks for the answer, I am still learning the string slicing function hence was hesitant to use the sting slicing, but your example cleared out the doubt I had and helped me understand it perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.