Printing multiple lists in one string - python

I have two separate lists of users (a person's name) and email addresses. How do I modify this
message = """To:
Hey,
How is the weather?"""
to print without changing
print(message)
Basically I'm looking for it to print out something similar to this without modifying the print(message).
To: bob#gmail.com
Hey, Bob
How is the weather?
I'm sorry if this sounds dumb, but I feel like I'm just beating my head against a wall here and cannot figure it out.

You can use str.format() as a means of string interpolation.
email = "bob#gmail.com"
name = "Bob"
message = """To: {}
From: {}
How's the weather""".format(email, name)
print(message)
The above code will output:
To: bob#gmail.com
From: Bob
How's the weather

Here is one way of achieving the desired result
email = "bob#gmail.com"
user = "Bob"
message = """To: {} \nFrom: {} \nHow is the weather?""".format(email, name)
print(message)
To: bob#gmail.com
From: Bob
How is the weather?
But I think you may be interested in the input function that creates a text box and you fill it. Here is how it works.
input('Do you want to write a message? : ')
input('Tell us who is receiving the message: ')
input('Now write your message in this box: ')
Here are the answers that I put in ['Yes', 'John', 'Hi John, this is Samuel Nde.']. The output is below.
Do you want to write a message? : Yes
Tell us who is receiving the message: John
Now write your message in this box: Hi John, This is Samuel Nde.
'Hi John, This is Samuel Nde.'

Related

Printing a list to new line for each set of data

I am still learning coding so please forgive me if this is basic. I have a set of code that asks the user how many users it wants to input (x) and asks for basic information about all X of those students (first and last name, age). However, when I print it out it all comes out as one long line. I have seen a few ways to print each character to a new line but not each new set of data. I would like to separate it by user. What I currently have is:
Name: Age:
Tiger woods 40, karen woods 33, charlie brown 44
What I would like it:
Name: Age:
Tiger Woods 40
Karen Woods 33
Charlie Brown 44
This is the code I am currently working with:
list.append(firstname)
list.append(lastname)
list.append(age)
print("name: age:")
print(", ".join(map(str, list)))
The simplest way is to use \n in a string to start a new line. For example, if you have this print statement:
print("Hello world!\nHow are you today?")
It would display as:
Hello world!
How are you today?
Thus, you should simply be able to replace this:
print(", ".join(map(str, list)))
with this:
print("\n".join(map(str, list)))
or something of that nature.
Try using a list of touples instead a list of words
list.append((firstname,lastname,age))
edited
sorry, you can access the data through a subindex
you_list[0] #for firstname
you_list[2] #for age

I need some help to fix my code to the right way

i need to translate from one to another language . what did i do wrong ?
language={}
language = {"Bounjour" : 'Hello',
"Comment allez vous?" : 'How are you?',
"Aurevoir" : 'Good Bye'
#User input
print 'Bounjour, Comment Allez vous, Aurevoir'
phrase = raw_input('Please enter a phrase to translate: ')
#result
print "Your sentence in English: ",
for phrase in language:
translates = language[words]
print translates
I see three errors:
The user's input is saved in a variable named phrase, but then the for loop uses that same variable as its iterator, so the user input is discarded.
words is not defined anywhere.
translates is not defined anywhere.
But beyond those errors, you don't even need a loop; just print language[phrase].

Python: How do I write a message to items in a list without printing each message individually?

In Python3, I'd like to write a blanket message that goes to all items in the list (names of dinner guests, in this case) without having to print each message individually.
For example, if the list is:
guest_list = ['John', 'Joe', 'Jack']
I want it to print this line below using each person's name without having to individually print the message 3 times:
print("Hello, " + *name of guest from the list above here* + "! We have found a bigger table!")
Desired Result:
Hello, John! We have found a bigger table!
Hello, Joe! We have found a bigger table!
Hello, Jack! We have found a bigger table!
Is this possible? If so, how? Thanks to any help offered!
If you want to do only one print:
guest_list = ['John', 'Joe', 'Jack']
result_str = ['Hello {}! We have found a bigger table!'.format(guest) for guest in guest_list]
print(result_str.join('\n'))
you can do the following:
for x in guest_list:
print("Hello, %s! We have found a bigger table!" %x)
Where %s allows you to insert a string format which is replaced by the variable I am passing to it.
You can use a simple for loop:
guest_list = ['John', 'Joe', 'Jack']
for x in guest_list:
print("Hello, " + x + "! We have found a bigger table!")
Hello, John! We have found a bigger table!
Hello, Joe! We have found a bigger table!
Hello, Jack! We have found a bigger table!

Need to listing user input in python

I need help figuring out how to turn a simple user input like
a = input('Enternumber: ')
and if the user was to input say...
hello bob Jeff Lexi Ava
How am I supposed to have the computer turn that into a list like,
hello
bob
Jeff
Lexi
Ava
If someone has the code could they please explain what they are doing. *This is python
Use the split method.
my_string = 'hello bob Jeff Lexi Ava'
print(my_string.split()) # ['hello', 'bob', 'Jeff', 'Lexi', 'Ava']
To print each on a line:
for word in my_string:
print(word)

Splitting a text

I have a large text in which three people talking.
I read that text to a string variable in python.
Text is like
JOHN: hello
MIKE: hello john
SARAH: hello guys
Imagine a long talk between 3 people. I want to split the texts into lists like
john = []
mike = []
sarah = []
and I want the list john to contain every sentence john said.
Can anyone help me with the code I need?
See if this is enough to get you started.
for line in text:
if line.startswith('JOHN'):
john.append(line)
elif line.startswith('MIKE'):
mike.append(line)
elif line.startswith('SARAH'):
sarah.append(line)

Categories

Resources