A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Create a new string made of the first, middle, and last characters of each input string using python programming
Q:

Create a new string made of the first, middle, and last characters of each input string using python programming

0

Create a new string made of the first, middle, and last characters of each input string

Given two strings, s1 and s2, write a program to return a new string made of s1 and s2’s first, middle, and last characters.

Given:

s1 = "America"
s2 = "Japan"

Expected Output:

AJrpan

All Answers

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

Hint:

  • String index starts with index 0. The first character is present at index 0, and the last character is at the index string’s length -1
  • Use built-in function len(s1) to get the string length.
  • Next, get the middle index number by dividing string length by 2.

Solution:

  • Get the first character from both strings, concatenate them, and store them in variable x
  • Get the middle character from both strings, concatenate them, and store them in variable y
  • Get the last character from both strings, concatenate them, and store them in variable x
  • In the end, join xy, and z and save it in the result variable
  • print the result
def mix_string(s1, s2):
    # get first character from both string
    first_char = s1[0] + s2[0]

    # get middle character from both string
    middle_char = s1[int(len(s1) / 2):int(len(s1) / 2) + 1] + s2[int(len(s2) / 2):int(len(s2) / 2) + 1]

    # get last character from both string
    last_char = s1[len(s1) - 1] + s2[len(s2) - 1]

    # add all
    res = first_char + middle_char + last_char
    print("Mix String is ", res)

s1 = "America"
s2 = "Japan"
mix_string(s1, s2)

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now