Q:

Python program to remove space between tuple elements

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

Now, let's get back to our topic of extracting even indexed elements in Tuple.

Removing space between tuple elements

For this, we will traverse the tuples and remove the spaces while printing them.

All Answers

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

Method 1:

To remove the extra space between tuples, we will simply use the replace() method which will replace the space between elements of tuple and then convert it string using the str() method.

# Python program to remove spaces 
# between elements of tuple 

# Creating and printing tuple...
myTuple = (6, 21, 8, 1, 9, 3)
print("Initial Tuple : " + str(myTuple))

# Removing spaces between elements of tuple 
spaceRemTuple = str(myTuple).replace(' ', '')

# printing space eliminated tuple 
print("Spaced Removed Tuple : " + str(spaceRemTuple))

Output:

Initial Tuple : (6, 21, 8, 1, 9, 3)
Spaced Removed Tuple : (6,21,8,1,9,3)

Method 2:

One more method to solve the problem is by removing the extra space from the tuple by using the join() method and then map it using the map() method.

# Python program to remove spaces 
# between elements of tuple 

# Creating and printing tuple...
myTuple = (6, 21, 8, 1, 9, 3)
print("Initial Tuple : " + str(myTuple))

# Removing spaces between elements of tuple 
spaceRemTuple = "(" + ",".join(map(str, myTuple)) + ")"

# printing space eliminated tuple 
print("Spaced Removed Tuple : " + str(spaceRemTuple))

Output:

Initial Tuple : (6, 21, 8, 1, 9, 3)
Spaced Removed Tuple : (6,21,8,1,9,3)

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

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to print all pair combinations of e... >>
<< Python program to get even indexed elements in tup...