Q:

Create a mixed String using the following rules using python programming

belongs to collection: Python String Exercises

0

Create a mixed String using the following rules

Given two strings, s1 and s2. Write a program to create a new string s3 made of the first char of s1, then the last char of s2, Next, the second char of s1 and second last char of s2, and so on. Any leftover chars go at the end of the result.

Given:

s1 = "Abc"
s2 = "Xyz"

Expected Output:

AzbycX

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

s1 = "Abc"
s2 = "Xyz"

# get string length
s1_length = len(s1)
s2_length = len(s2)

# get length of a bigger string
length = s1_length if s1_length > s2_length else s2_length
result = ""

# reverse s2
s2 = s2[::-1]

# iterate string 
# s1 ascending and s2 descending
for i in range(length):
    if i < s1_length:
        result = result + s1[i]
    if i < s2_length:
        result = result + s2[i]

print(result)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to check if two strings are... >>
<< Count all letters, digits, and special symbols fro...