0

Hi im new to Python and Ive been trying to get format print to work but, and this may be me being new, but it seems to be very badly implemented.Any examples for 2.7.6 dont work for the new version and their aren't any real examples I could find on the internet for 3.3. As such I would like to ask for a good example of how format string works. For instance ive been trying to get this to work from my homework.

day,date,year,hour,and minutes must be separate variables.

using one formatted print statement,print the following:

Date:5/31/2013
Time: 3:45 pm

I can get it to work with this code:

def date():

    Month=5
    Day=31
    Year=2013
    Hours=3
    Minutes=45
    Scale='pm'

    a="Date: %i/%i/%i\nTime: %i:%i %s" %(Month,Day,Year,Hours,Minutes,Scale)
    print(a)

It works but its not one line as asked for. Please help format is so confusing.

3
  • 4
    just use :print("Date: %i/%i/%i\nTime: %i:%i %s" %(Month,Day,Year,Hours,Minutes,Scale)) , no need to store the result in a variable. Commented Jun 9, 2013 at 16:08
  • 2
    Welcome to Stack Overflow. Please read the FAQ soon. When someone describes a well-known, widely used product with comments like "I'm trying to get XXX to work ...it seems to be very badly implemented", you immediately reduce your chances of getting useful help. People will either be insulted and respond contemptuously or will just laugh at you and ignore the question. There's a category for closing questions of 'Not constructive' with the rubric "...but this question will likely solicit debate, arguments, polling, or extended discussion." Beware! Commented Jun 9, 2013 at 16:14
  • Just to note this, the "%i" and similar placeholders are one way to insert things. The other way is to use "{}", which is to me much easier to read and write and in most cases, the default conversion is exactly what you want. Anyway, welcome to Python! Commented Jun 9, 2013 at 16:14

2 Answers 2

3

The \n in your format string is inserting the new line character. Remove the \n, and you will not have the newline any longer.

Characters preceded by a backslash are known as escape characters. They can be used to insert special formatting into strings. For example:

  • \n is the newline character,
  • \t is the tab character
Sign up to request clarification or add additional context in comments.

Comments

0

Remove \n because that is used to create a line break.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.