Python Forum
print values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print values (/thread-8526.html)



print values - volcano - Feb-24-2018

As per my understanding if we want to add two strings and print then we should use comma .

e.g print('abc','def') = > this will give abcdef

But in this python code below we are not using comma but a "+"
classmates = {'Tony': ' cool but smells', 'Emma': ' sits behind me', 'Lucy': ' asks too many questions'}

for k, v in classmates.items():
    print(k + v)
Here we are using "+" in print ..instead of "comma" ...Is it correct ?


RE: print values - largecat - Feb-24-2018

'+' is to put some strings together,
so it's ok.

a='abc'
b='def'
print(a+b) #abcdef


RE: print values - wavic - Feb-24-2018

When you use a comma, you provide multiple parameters to print function for printing.
print(k, v) will print the key, then the value.

The + sign concatenates strings when is used with strings. Because of how the concatenation works you are forced to put an interval at the beginning of the value. You do not want to get 'Tonycool but smells', right? :)
But since the values have an interval at the beginning you get what you want concatenating them.

The better way is to use string formatting: '{} {}'.format(k, v).
The {} inside the string are placeholders for the format method parameters k and v in this case. Notice the interval between the placeholders. You are constructing the string the way you want to looks like. So it's not necessary to have an interval at the beginning of the value.

classmates = {'Tony': 'cool but smells', 'Emma': 'sits behind me', 'Lucy': 'asks too many questions'}

for k, v in classmates.items():
    print('{} {}'.format(k, v))
The output is:
Output:
Emma sits behind me Lucy asks too many questions Tony cool but smells
The order is different because I use Python 3.5.2 here not 3.6.1


RE: print values - snippsat - Feb-24-2018

(Feb-24-2018, 05:13 AM)volcano Wrote: e.g print('abc','def') = > this will give abcdef
No that's not correct it will give result with space,to get that result have to use sep=''.
>>> print('abc','def')
abc def
>>> print('abc','def', sep='')
abcdef
But to talk about what to do,so is string formatting the way to go,as @wavic show over.
Using + can get very ugly soon in a longer string.
>>> s_1 = 'bad'
>>> s_2 = 'way'
>>> answer = 42
>>> print('This is a ' + s_1 + ' ' + s_2 + ' to do string formatting' + ' and answer is ' + str(answer))
This is a bad way to do string formatting and answer is 42
So in Python 3.6 got f-string,so if have that version use it.
>>> s_1 = 'good'
>>> s_2 = 'way'
>>> answer = 42
>>> print(f'This is a {s_1} {s_2} to do string formatting and answer is {answer}')
This is a good way to do string formatting and answer is 42
10-year a ago in version 2.6 we got .format(),lower version than 3.6 use this.
>>> s_1 = 'good'
>>> s_2 = 'way'
>>> answer = 42
>>> print('This is a {} {} to do string formatting and answer is {}'.format(s_1, s_2, answer))
This is a good way to do string formatting and answer is 42

classmates = {'Tony': 'cool but smells', 'Emma': 'sits behind me', 'Lucy': 'asks too many questions'}

for k, v in classmates.items():
    print(f'<{k}> --> {v}')
Output:
<Tony> --> cool but smells <Emma> --> sits behind me <Lucy> --> asks too many questions