Search code examples
pythonarraysdictionaryremoving-whitespace

How to get rid of whitespace in element of dictionary?


I am trying to extract only the string without whitespace of an element in a dictionary. This is how my element looks currently:

u'GYM-7874 '

I only want to extract the value without the whitespace so "GYM-7874" only so that I can compare it with another list of strings. I tried using the .strip() method to get rid of whitespaces but unfortunately that only works with string. How do I get rid of the white space and only extract the characters?

# json_ob is the string array of dictionary 
title = json_ob[index]["title"]
title.strip()

Solution

  • strip() doesn't modify the string in place (strings are immutable in Python). You need to assign the result back to the variable.

    title = title.strip()