0

im new to python. My code is written below, i want to know if there is any way of writing in a single print instead of many print statements to get output like(with design)


  • Car Company: Honda
  • Model :City
  • Year Of Manufacturing: 2004
  • Color:Black
class car:
    def __init__(self,model,color):
        self.model=model
        self.color=color
    def carDetails(self):
        yearOfManufacturing=input("Enter Year of Manufacturing: ")
        carCompany='Honda'
        print("*********************************************")
        print("* Car Company: "+carCompany)
        print("* Model :"+self.model)
        print("* Year Of Manufacturing: " +yearOfManufacturing)
        print("* Color:" + self.color)
        print("*********************************************")
carObj=car('City','Black')
carObj.carDetails()
Craig
  • 4,605
  • 1
  • 18
  • 28
Sandy
  • 57
  • 1
  • 8
  • 2
    `\n` is the newline character. You can include it in the string you print to move on to the next line. – Craig Mar 17 '20 at 02:31
  • 3
    Does this answer your question? [How do I specify new lines on Python, when writing on files?](https://stackoverflow.com/questions/11497376/how-do-i-specify-new-lines-on-python-when-writing-on-files) – Pushkin Mar 17 '20 at 02:33
  • 3
    Kindly search before asking – Pushkin Mar 17 '20 at 02:34
  • 2
    Why do you want to do it in one line? Your current code is much more readable. – Selcuk Mar 17 '20 at 02:44

3 Answers3

2

The \n character is for a new line in a string:

class car: 

    def __init__(self, model, color): 
        self.model = model 
        self.color = color 

    def carDetails(self):

        yearOfManufacturing = input("Enter Year of Manufacturing: ") 
        carCompany = 'Honda'

        print(f"****************************\n* Car Company: + {carCompany}\n* Model : {self.model}\n* Year Of Manufacturing: {yearOfManufacturing}\n* Color: {self.color}\n****************************")


carObj = car('City', 'Black')
carObj.carDetails()
Oliver.R
  • 1,282
  • 7
  • 17
rrswa
  • 1,005
  • 1
  • 9
  • 23
2

I'd suggest using triple quotes with f-string notation for this:

print(
f"""
*********************************************
* Car Company: {carCompany}
* Model : {self.model}
* Year Of Manufacturing: {yearOfManufacturing}
* Color: {self.color}
********************************************
"""
)
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
1

The \n prints a new line.

e.g. print("line1 \nline2") will print

line1
line2
Alex Hawking
  • 1,125
  • 5
  • 19
  • 35