Silly begginer question

This is the place for queries that don't fit in any of the other categories.

Silly begginer question

Postby backwoodspimp » Wed Feb 20, 2013 9:11 pm

So I need to make this

Code: Select all
data = ("John", "Doe", 53.44)
format_string = "Hello"

print format_string % data


have the output of this

"Hello John Doe. Your current balance is 53.44$."

It is part of a begginer online tutorial and I am required to use string formatting as you can probably tell. I do not know how to call from a specific part of data. I keep getting syntax errors like this \/
File "<string>", line 3
print format_string " %s %s, Your current balence is %d$." % data
^
SyntaxError: invalid syntax
backwoodspimp
 
Posts: 1
Joined: Wed Feb 20, 2013 9:07 pm

Re: Silly begginer question

Postby KevinD » Wed Feb 20, 2013 10:01 pm

backwoodspimp wrote:So I need to make this

Code: Select all
data = ("John", "Doe", 53.44)
format_string = "Hello"

print format_string % data


have the output of this

"Hello John Doe. Your current balance is 53.44$."

It is part of a begginer online tutorial and I am required to use string formatting as you can probably tell. I do not know how to call from a specific part of data. I keep getting syntax errors like this \/
File "<string>", line 3
print format_string " %s %s, Your current balence is %d$." % data
^
SyntaxError: invalid syntax



Here's the general method for string formatting in 2.x; for version 3, someone else will have to answer.

To insert variables into a string, you would do this:
Code: Select all
>>> value1 = 'John'
>>> value2 = 'Doe'
>>> print 'some stuff with %s %s inserted' % (value1, value2)
some stuff with John Doe inserted
>>>
You insert the placeholders (%s) in the string you want, then the %, followed by the values you want to insert in parentheses. If there is only one value to insert, then the parentheses are optional. The number of placeholders and the number of values must match. Your specific problem would have 3 "%" placeholders in your string, and 3 values in the parentheses.

The "%s" indicates that you are inserting a string variable. You can use "%f" for a float variable (like a dollar amount), or "%i" for an integer, in addition to "%s". Look in the Python docs for more details about other value types you can insert.
Quanto lignum posset materiae materietur marmota Chuck si materiam possit materiari foedans, penitus lignum?
KevinD
 
Posts: 30
Joined: Fri Feb 08, 2013 3:15 am

Re: Silly begginer question

Postby Mekire » Wed Feb 20, 2013 11:41 pm

KevinD wrote:Here's the general method for string formatting in 2.x; for version 3, someone else will have to answer.

Here is your example using the newer string formatting but both the % and format function both work in py2.7 and 3.x.
Code: Select all
>>> value1 = 'John'
>>> value2 = 'Doe'
>>> print("some stuff with {} {} inserted".format(value1,value2))
some stuff with John Doe inserted
>>>

As long as you surround your print statement in parenthesis it will work in py2.7 and 3.x without problems.

Details of formatting with the above function here: Format Specification Mini-Language

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Silly begginer question

Postby ichabod801 » Thu Feb 21, 2013 12:44 am

Please post complete tracebacks in code tags. It looks from what you posted that the syntax error is at the beginning of the line. That is generally a sign that the syntax error is on the previous line, where you forgot to close a parentheses or a bracket. However, I expect it's because you have a variable (format_string) next to a string literal ("Hello ..." without an operator in between.

Complete tracebacks with the code that actually caused the error can help us pinpoint the problem much better.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron