Python Forum

Full Version: week 1 python help - string formatting ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Thanks in advance. I am stuck on a part of my homework (I know this is basic stuff)
this is the assignment:
Your program should ask the user for his/her first name using the Python input function.
Your program should ask the user for his/her last name using the Python input function.
Your program should create two greetings using the user’s first and last name that look like this:
SEC 290 welcomes Cameron Diaz to the World of Python, assuming the user’s name is Cameron Diaz.
The first greeting should be constructed using string concatenation.
The second greeting should be constructed using the string format method.
Display each greeting using a print statement.

This is what I have so far. I am able to get everything except the string formatted method.
# Sec 290
# July 08, 2019
# Week 1 Programming assignment


first = input("What is your first name:")
last = input("What is your last name:")
# string concatenation

print("Sec290 welcomes "  +  first , last )

# string format

print("Sec290 welcomes 

so after reading my post to ensure accuracy, I noticed I forgot to put (to the World of Python) in my first greeting. I attempted to do the following but it does not work so that may be problem #2
print("Sec290 welcomes "  +  first , last "to the World of Python ")
Here is a link to the forums tutorial on string formatting
https://python-forum.io/Thread-Basic-str...xpressions
The basic use of the string formatting method is to just put curly braces ({}) in your string where you want the text to go, and call the format method with the text to use as parameters:

name = 'Arthur'
title = 'King of the Britons'
print('I am {}, {}.'.format(name, title))
You can see the full format method syntax here.
disregard the reply I made about forgetting part of the greeting I did figure it out. Just need the formatting string part. Below is my fix
so far

print("Sec290 welcomes "  +  first , last  +  " to the World of Python ")

(Jul-08-2019, 09:41 PM)Yoriz Wrote: [ -> ]Here is a link to the forums tutorial on string formatting
https://python-forum.io/Thread-Basic-str...xpressions

thank you very much for the link but I am just not getting it...I know it will be like dah once I see it but I just can not get the desired results or understand how
Yoriz and ichabod gave you links as well as examples for you to replicate your code to work correctly as intended. I am not sure how any more would help other than to give you the answer...which i wont. I would suggest to read the links they gave you. If you did read it you could answer your own question.
(Jul-08-2019, 09:46 PM)raymond2688 Wrote: [ -> ]I just can not get the desired results

That sounds like you tried something. Show us what you tried, tell us how it failed.
thanks, I have been ready but it is not in language for someone who is 1 day into python...lol I will keep plugging away at it. I got everything in the exercise except for format string. I just can't seem to figure out how to make {} those associated with first and last name.
Look at my example: that's exactly what I do. I associate two variables with two sets of {}.
(Jul-08-2019, 11:07 PM)ichabod801 Wrote: [ -> ]
(Jul-08-2019, 09:46 PM)raymond2688 Wrote: [ -> ]I just can not get the desired results

That sounds like you tried something. Show us what you tried, tell us how it failed.

Below is what I just tried. I feel I'm close but not getting it...the problem for me is the last string

first = input("What is your first name:")
last = input("What is your last name:")
# string concatenation

print("Sec290 welcomes "  +  first , last  +  " to the world of Python ")

# string format

print("Sec290 welcomes" '{} {}'.'.format (first, last) "to the world of Python")
The format part goes right at the end of the line after all of the string stuff.
Think of the string being the whole line of text, the {} just go where you want to replace stuff in the string and in the () of format goes the stuff that replaces the {}
Example
print('text here replace more text')
print('text here {} more text'.format('changed'))
print('text here {} more text {}'.format('changed', 'new'))
Output:
text here replace more text text here changed more text text here changed more text new
Pages: 1 2 3