Python Forum
week 1 python help - string formatting ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
week 1 python help - string formatting ?
#1
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 ")
Reply
#2
Here is a link to the forums tutorial on string formatting
https://python-forum.io/Thread-Basic-str...xpressions
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
Reply
#5
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.
Recommended Tutorials:
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply
#8
Look at my example: that's exactly what I do. I associate two variables with two sets of {}.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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")
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String formatting - tax brackets darek88 1 2,005 Aug-28-2020, 09:59 AM
Last Post: Larz60+
  Calculating the number of day of the week RbaPhoenix 3 2,500 Mar-27-2020, 09:23 PM
Last Post: Larz60+
  String formatting issues? lolatyou 2 2,082 Mar-08-2020, 06:53 AM
Last Post: michael1789
  hardest week yet: some guidence please raymond2688 25 10,739 Aug-22-2019, 05:23 PM
Last Post: ichabod801
  Trying to get the week number from a string of numbers fad3r 2 3,203 Apr-15-2018, 06:52 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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