0

I am trying to remove the extra space from the data which is entered as input.

I tried using the .strip() condition but it doesn't remove the extra space of the entered data.

the code I wrote to remove extra space

Data_1 = input("Enter your name:", )
print(Data_1.strip())
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Your code is working as expected. Please provide the input data that has an issue. – Rima Mar 13 '21 at 19:43
  • 2
    Welcome to SO! Check out the [tour]. Please provide a [mre] including input, expected output, and actual output. You can [edit] the question. Check out [ask] if you want more tips. – wjandrea Mar 13 '21 at 19:44
  • You might be looking for this: [Is there a simple way to remove multiple spaces in a string?](https://stackoverflow.com/q/1546226/4518341) – wjandrea Mar 13 '21 at 19:46

2 Answers2

0

These are 3 ways of dealing with spaces you can use according to your needs:

1- the method you are using

.strip() method removes whitespaces at the beginning and end of your input string.

if your input string Data_1 is " john " or "Edward luther " or " Stephanie"

doing Data_1.strip() returns "john" or "Edward luther" or "Stephanie" with no extra spaces at the beginning and at the end

2- removing all spaces

if what you want is remove all the spaces inside a string like "Edward Hamilton luther "

you can simply use Data_1.replace(" ", ""), that will remove all whitespaces and result in "EdwardHamiltonluther"

3- removing excessive whitespaces

if you want to remove excessive whitespaces in a string for example " Edward Hamilton is a nice guy "

you can use " ".join(Data_1.split())

it will return "Edward Hamilton is a nice guy"

try it :

Data_1 = input("Enter your name:", )
print(" ".join(Data_1.split()))
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Elyes Lounissi
  • 405
  • 3
  • 12
0

.strip() removes the whitespaces in the front and end of the string. To remove the extra spaces, you can try replace them.

Data_1 = input("Enter your name:", )
print(Data_1.replace("  ", "").strip())

Output -

Enter your name:  E  lon Mu  s  k 
Elon Musk
indraneel
  • 66
  • 3