top of page

Python Programming Help

Public·2 members

Concatenate First, mid and last character of String in Python

Given a String, Extract first, mid and last character of the string and return concatenate of these characters


Input 1:

Enter the String: codersarts
Input : codersarts
Output:  css

Input 2:

Enter the String: hello
Input : hello
Output:  hlo

Here is Python code Solution

# input the string
input_str=input("Enter the String: ")

output_str=""

if(len(input_str)==1):
    output_str=input_str[0]
elif len(input_str)==2:
    output_str=input_str[0]+input_str[1]
elif len(input_str)>2:
    output_str = input_str[0] + input_str[len(input_str)//2] + 
    input_str[len(input_str)-1]
else:
    print("Please enter valid String not empty string")
    
print("Input :",input_str)    
print("Output: ",output_str)

195 Views
bottom of page