;

Python program to Remove Characters from an Odd Index Positions in a String


Tutorialsrack 23/04/2020 Python

In this python program, we will learn how to remove the character from an Odd index position in a string. 

Here is the source code of the program to remove the character from an Odd index position in a string. 

Program 1: Python program to Remove Characters from an Odd Index Positions in a String Using For Loop

Python program to Remove Characters from an Odd Index Positions in a String Using For Loop

# Python program to Remove Characters from an Odd Index Positions in a String Using For Loop

# Take the Input From the User
str1 = input("Enter the String: ")

str2 = ''

for i in range(1, len(str1) + 1):
    if(i % 2 == 0):
        str2 = str2 + str1[i - 1]
        
print("\nOriginal String before Removing Characters from Odd Positions:  ", str1)
print("Final String After Removing Characters from Odd Positions:      ", str2)

Output

Enter the String: Tutorialsrack is a online learning website

 

Original String before Removing Characters from Odd Positions:   Tutorialsrack is a online learning website

Final String After Removing Characters from Odd Positions:       uoilrc saoln erigwbie

Program 2: Python program to Remove Characters from an Odd Index Positions in a String Using While Loop

Python program to Remove Characters from an Odd Index Positions in a String Using While Loop

# Python program to Remove Characters from an Odd Index Positions in a String Using While Loop

# Take the Input From the User
str1 = input("Enter the String: ")

str2 = ''
i = 1
while(i <= len(str1)):
    if(i % 2 == 0):
        str2 = str2 + str1[i - 1]
    i = i + 1
        
print("\nOriginal String before Removing Characters from Odd Positions:  ", str1)
print("Final String After Removing Characters from Odd Positions:      ", str2)
Output

Enter the String: Tutorialsrack is a online learning website

 

Original String before Removing Characters from Odd Positions:   Tutorialsrack is a online learning website

Final String After Removing Characters from Odd Positions:       uoilrc saoln erigwbie


Related Posts



Comments

Recent Posts
Tags