Python Forum
break print_format lengthy line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
break print_format lengthy line
#1
hi
in the below snippet code :

info1={'name':'ali', 'classroom':3}
info2={'name':'mohammad','classroom':2}
print('{0[name]} is in  classroom {0[classroom]}.'.format(info1)) # 0 refers to info1
# note name and classroom in{} are without ''


print('{0[name]} is in  classroom {0[classroom]} and  {1[name]} is in  classroom {1[classroom]}.'.format(info1,info2))
# 0 refers to info1 and 1 refers to info2 .
how can break the lengthy last line(only with one format)?
thanks
Reply
#2
Can do it like this.
info1 = {'name': 'ali', 'classroom': 3}
info2 = {'name': 'mohammad', 'classroom': 2}
for info in [info1, info2]:
    print('{name} is in classroom {classroom}.'.format(**info))
Output:
ali is in classroom 3. mohammad is in classroom 2
Also if add more data this soon get ugly,then would something like this be better.
import pandas as pd

# List of dictionaries
infos = [
    {'name': 'ali', 'classroom': 3},
    {'name': 'mohammad', 'classroom': 2},
    {'name': 'Kent', 'classroom': 5}
    # Add more dictionaries as needed
]

df = pd.DataFrame(infos)
print(df)
Output:
name classroom 0 ali 3 1 mohammad 2 2 Kent 5
Reply
#3
Change the input to keep it short and sweet?

infos = [('Ali', 3), ('Mohammed', 2), ('Kent', 5), ('Barbie', 0)]
for i in infos:
    print('{} is in classroom: {}.'.format(*i))
for i in infos:
    print(f'{i[0]} is in classroom: {i[1]}.')
Thought of this:

infos = [('Ali', 3), ('Mohammed', 2), ('Kent', 5), ('Barbie', 0), ('Spiderman', 0)]
for i in range(len(infos) - 1):
    print('{} is in classroom: {} and {} is in classroom {}'.format(*infos[i], *infos[i+1]))
Reply
#4
The strings in this example are concatenated when the module is parsed..
info1 = {'name': 'ali', 'classroom': 3}
info2 = {'name': 'mohammad', 'classroom': 2}

print(
    "{0[name]} is in  classroom {0[classroom]} and  "
    "{1[name]} is in  classroom {1[classroom]}."
    .format(info1, info2)
)
Output:
ali is in classroom 3 and mohammad is in classroom 2.
akbarza likes this post
Reply
#5
(Mar-12-2024, 05:07 PM)deanhystad Wrote: The strings in this example are concatenated when the module is parsed..
info1 = {'name': 'ali', 'classroom': 3}
info2 = {'name': 'mohammad', 'classroom': 2}

print(
    "{0[name]} is in  classroom {0[classroom]} and  "
    "{1[name]} is in  classroom {1[classroom]}."
    .format(info1, info2)
)
Output:
ali is in classroom 3 and mohammad is in classroom 2.

thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print a line break in writelines() method leodavinci1990 1 6,481 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Regex won't replace character with line break Tomf96 2 2,561 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  Break Line (For Debugging) vetabz 7 4,568 May-08-2017, 02:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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