-2

I have a variable:

exchange_name = ''

Now I want to perform some operation based on checking if it is equal to an empty string.

So, I do:

if exchange_name == '':
  # perform some operation

But how can I generalize the solution so that exchange_name can contain any number of spaces, e.g.:

exchange_name = ' '

or

exchange_name = '  '

Can anybody suggest an approach? Thanks in advance.

cmaher
  • 4,853
  • 1
  • 20
  • 34
megna
  • 185
  • 2
  • 14
  • You just need to trim white space from your variable and compare that result to the empty string. The strip function will do this for you. – Brian Driscoll Apr 26 '18 at 01:11

2 Answers2

3
exchange_name.strip()==''

strip removes all empty spaces.

glycoaddict
  • 553
  • 4
  • 16
1

Try to use rstrip to remove spaces from begin and end of string.

if mytext.rstrip() == '':
    do_it()
Willian Vieira
  • 513
  • 2
  • 8