0

Here is the code in my test.py file.

print ('The 2nd arg is: \'', sys.argv[1], '\'.')

Each time I run this command

python test.py hello

I get this

The 2nd arg is: ' hello '.

There are whitespaces before and after the string. How do I get rid of them?

I've tried strip() which doesn't work.

PutBere
  • 131
  • 1
  • 12
  • The comma in a print statement adds spaces. Use the "+" operator to concatenate without spaces. – Suraj Kothari Dec 22 '20 at 14:23
  • Does this answer your question? [How to print variables without spaces between values](https://stackoverflow.com/questions/28669459/how-to-print-variables-without-spaces-between-values) – Tomerikoo Dec 22 '20 at 14:30
  • @SurajKothari ...or just set `sep=''`... – Tomerikoo Dec 22 '20 at 14:30
  • From the [docs](https://docs.python.org/3/library/functions.html#print): *"`print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)` - Print `objects` to the text stream `file`, __separated by `sep`__ and followed by `end`"* – Tomerikoo Dec 22 '20 at 14:39

2 Answers2

1

Issue is in your print function. You are getting extra white-spaces because of the , inside your print. This should fix the issue:

print ('The 2nd arg is: \'' + sys.argv[1] + '\'.')
Jarvis
  • 8,494
  • 3
  • 27
  • 58
1

you can use following formatting:

print ("The 2nd arg is:'{}'.".format(sys.argv[1]))