Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print values
#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


Messages In This Thread
print values - by volcano - Feb-24-2018, 05:13 AM
RE: print values - by largecat - Feb-24-2018, 06:43 AM
RE: print values - by wavic - Feb-24-2018, 07:22 AM
RE: print values - by snippsat - Feb-24-2018, 02:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Print names in x-axis of a time-series values hobbyist 4 1,303 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,820 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Print variable values from a list of variables xnightwingx 3 2,701 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,515 Jul-12-2018, 09:03 AM
Last Post: buran
  print 3 return values from function bluefrog 5 5,348 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