Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print values
#1
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 ?
Reply
#2
'+' is to put some strings together,
so it's ok.

a='abc'
b='def'
print(a+b) #abcdef
Reply
#3
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print names in x-axis of a time-series values hobbyist 4 1,221 Apr-22-2023, 09:29 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,748 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Print variable values from a list of variables xnightwingx 3 2,640 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 2,436 Jul-12-2018, 09:03 AM
Last Post: buran
  print 3 return values from function bluefrog 5 5,260 Mar-05-2017, 07:58 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020