How to Call the Print Statement Again
Python is a versatile and flexible linguistic communication – there is often more than one way to achieve something.
In this tutorial, you'll come across some of the ways you tin impress a string and a variable together.
Let's get started!
How to use the print() function in Python
To impress anything in Python, you use the impress() role – that is the print keyword followed by a gear up of opening and endmost parentheses,().
#how to print a cord print("Hello world") #how to print an integer print(seven) #how to print a variable #to merely impress the variable on its own include only the name of it fave_language = "Python" print(fave_language) #output #Howdy world #7 #Python If yous omit the parentheses, y'all'll get an error:
impress "howdy earth" #output later running the code: #File "/Users/dionysialemonaki/python_articles/demo.py", line 1 # print "hello globe" # ^^^^^^^^^^^^^^^^^^^ #SyntaxError: Missing parentheses in telephone call to 'impress'. Did you mean print(...)? If you write your Python code in Visual Studio Code, with the Python extension, you'll also become an underline and a hint which all betoken that something is not quite right:
As mentioned above, the print statement is used to output all kinds of information. This includes textual and numerical data,variables, and other data types.
You lot tin can too print text (or strings) combined with variables, all in one argument.
You'll encounter some of the different means to do this in the sections that follow.
How to print a variable and a string in Python using concatenation
To concatenate, according to the dictionary, means to link (things) together in a chain or series.
You do this past adding various things (in this instance programming – y'all add information) together with one another, using the Python addition operator, +.
Proceed in listen that concatenation is only used for strings, and so if the variable you want to concatenate with the remainder of the strings is of an integer data type, you'll accept to convert it to a string with the str() function.
In the following example, I want to impress the value of a variable along with another text.
I add the strings in double quotes and the variable proper name without whatsoever surrounding it, using the addition operator to chain them all together:
fave_language = "Python" print("I similar coding in " + fave_language + " the most") #output #I like coding in Python the most With string concatenation, y'all take to add spaces by yourself, and so if in the previous case I hadn't included any spaces within the quotation marks the output would await like this:
fave_language = "Python" print("I like coding in" + fave_language + "the virtually") #output #I like coding inPythonthe most Y'all can fifty-fifty add the spaces separately:
fave_language = "Python" print("I similar coding in" + " " + fave_language + " " + "the near") #output #I similar coding in Python the most This is non the about preferred way of printing strings and variables, as it can be error decumbent and time-consuming.
How to print a variable and a cord in Python past separating each with a comma
You tin print text alongside a variable, separated by commas, in one print statement.
first_name = "John" print("Hullo",first_name) #output #Hello John In the example above, I outset included some text I wanted to impress in double quotation marks – in this example, the text was the string Hello.
After the closing quotation mark, I added a comma which separates that piece of text from the value held in the variable name (first_name in this case) that I then included.
I could take added more text following the variable, similar then:
first_name = "John" print("Hello",first_name,"expert to encounter you") #output #Hello John good to run into you This method also works with more than than one variable:
first_name = "John" last_name = "Doe" impress("Hello",first_name,last_name,"skilful to encounter you lot") #output Hi John Doe adept to see you lot Make sure to separate everything with a comma.
So, you separate text from variables with a comma, but as well variables from other variables, like shown above.
If the comma hadn't been added between first_name and last_name, the code would've thrown an error:
first_name = "John" last_name = "Doe" print("Hello",first_name last_name,"good to see you") #output #File "/Users/dionysialemonaki/python_articles/demo.py", line 4 # print("Hello",first_name last_name,"adept to see you") # ^^^^^^^^^^^^^^^^^^^^ #SyntaxError: invalid syntax. Perhaps yous forgot a comma? As you see, Python mistake letters are extremely helpful and make the debugging process a bit easier :)
How to print a variable and a string in Python using string formatting
You lot use string formatting past including a set of opening and closing curly braces, {}, in the place where y'all want to add the value of a variable.
first_name = "John" print("How-do-you-do {}, hope you're well!") In this case there is one variable, first_name.
Inside the print statement there is a set of opening and closing double quotation marks with the text that needs to be printed.
Inside that, I've added a fix of curly braces in the place where I desire to add together the value of the variable first_name.
If I try and run this lawmaking, it volition have the following output:
#output #Howdy {}, promise you're well! It doesn't actually print the value of first_name!
To print it, I need to add the .format() string method at the end of the string – that is immediately after the closing quotation mark:
first_name = "John" print("How-do-you-do {}, hope you're well!".format(first_name)) #output #Hello John, hope you're well! When there is more one variable, yous use as many curly braces as the number of variables you want to print:
first_name = "John" last_name = "Doe" print("Hullo {} {}, hope yous're well!") In this instance, I've created ii variables and I want to impress both, one after the other, so I added two sets of curly braces in the place where I want the variables to exist substituted.
At present, when information technology comes to the .format() method, the order in which you place the variable names within matters.
So, the value of the variable proper name that will exist added first in the method will be in the place of the first curly brace, the value of the variable name that will be added second volition be in the place of the second curly brace, and then on.
Make sure to separate the variable names by commas within the method:
first_name = "John" last_name = "Doe" print("Hello {} {}, hope you lot're well!".format(first_name,last_name)) #output #Hi John Doe, hope y'all're well! If I'd reversed the club of the names inside the method, the output would expect different:
first_name = "John" last_name = "Doe" print("Hello {} {}, hope yous're well!".format(last_name,first_name)) #output #Hi Doe John, hope yous're well! How to print a variable and a cord in Python using f-strings
f-strings are a better and more readable and concise way of achieving string formatting compared to the method we saw in the previous section.
The syntax is easier and requires less transmission work.
The full general syntax for creating an f-string looks like this:
print(f"I desire this text printed to the panel!") #output #I want this text printed to the console! You first include the graphic symbol f before the opening and endmost quotation marks, inside the print() function.
To print a variable with a string in one line, you again include the character f in the same identify – right earlier the quotation marks.
Then you add the text you lot desire inside the quotation marks, and in the place where yous want to add the value of a variable, yous add together a set of curly braces with the variable name inside them:
first_name = "John" print(f"Hello, {first_name}!") #output #Howdy, John! To print more than variable, you add another set of curly braces with the second variable name:
first_name = "John" last_name = "Doe" print(f"Hello, {first_name} {last_name}!") #output #How-do-you-do, John Doe! The club you place the variable names does thing, so brand sure you add together them according to the output you lot desire.
If I had reversed the gild of the names, I'd get the following output:
first_name = "John" last_name = "Doe" impress(f"Hello, {last_name} {first_name}!") #output #Hi, Doe John! Conclusion
Thanks for reading and making it to the stop! You at present know a few dissimilar means of printing strings and variables together in one line in Python.
If you lot want to learn more than virtually Python, check out freeCodeCamp's Python Certification.
It'southward suitable for beginners as information technology starts from the fundamentals and gradually builds to more advanced concepts. Y'all'll also go to build five projects and put to practise all the new noesis you larn.
Happy coding!
Learn to code for free. freeCodeCamp'due south open source curriculum has helped more than forty,000 people get jobs equally developers. Become started
Source: https://www.freecodecamp.org/news/python-print-variable-how-to-print-a-string-and-variable/
0 Response to "How to Call the Print Statement Again"
Enviar um comentário