-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
  • 5,100
  • 1
  • 22
  • 34
megna
  • 215
  • 3
  • 16
  • 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
  • 641
  • 6
  • 19
1

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

if mytext.rstrip() == '':
    do_it()
Willian Vieira
  • 646
  • 3
  • 9