{"id":21231,"date":"2022-11-24T19:25:26","date_gmt":"2022-11-24T13:55:26","guid":{"rendered":"https:\/\/java2blog.com\/?p=21231"},"modified":"2022-11-25T18:45:26","modified_gmt":"2022-11-25T13:15:26","slug":"print-variable-and-string-same-line-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/print-variable-and-string-same-line-python\/","title":{"rendered":"Print Variable and String in Same Line in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Print_Variable_and_String_in_Same_Line_in_Python\">Print Variable and String in Same Line in Python<\/a><ul><li><a href=\"#Using_a_comma\">Using a comma<\/a><\/li><li><a href=\"#Using_the_formatting\">Using the % formatting<\/a><\/li><li><a href=\"#Using_the_format_function\">Using the format() function<\/a><\/li><li><a href=\"#Using_the_fstrings\">Using the fstrings<\/a><\/li><li><a href=\"#Using_the_operator_and_str_function\">Using the + operator and str() function<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<blockquote><p>\n<span style=\"font-size: 1.2em\">&#x1f4a1; <strong>TL;DR<\/strong><\/style>\n<p><strong>To Print Variable and String in Same Line in Python, use <code>print()<\/code> method and separate String and variable with comma.<\/strong><\/p>\n<pre code = \"python\" title = \"Using a comma\">\na = 2\nprint(\"Java\", a, \"Blog\")    # Output: Java2Blog\n<\/pre>\n<\/blockquote>\n<h2><span id=\"Print_Variable_and_String_in_Same_Line_in_Python\">Print Variable and String in Same Line in Python<\/span><\/h2>\n<p>We use the <code>print()<\/code> function in Python to display output. It was changed to a function in Python  and existed as a statement in Python 2. Changing it to a function was very useful as it introduced parameters like <code>end<\/code>, <code>sep<\/code>, and more with this function.<\/p>\n<p>We can display multiple values at once using the <code>print()<\/code> function in Python.<\/p>\n<p>This tutorial will demonstrate how to print variable and string in same line in Python.<\/p>\n<h3><span id=\"Using_a_comma\">Using a comma<\/span><\/h3>\n<p>We can use a comma to print variable and string in same line in Python. We will separate the two values using the comma within the function.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title = \"Using a comma\">\na = 2\nprint(\"Java\", a, \"Blog\")    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nJava 2 Blog\n<\/div>\n<p>Note the space between the string and variable in the output. This is due to the <code>sep<\/code> parameter within the <code>print()<\/code> function which sets the separator character between two values and is set to a space character by default. <\/p>\n<h3><span id=\"Using_the_formatting\">Using the <code>%<\/code> formatting<\/span><\/h3>\n<p>Python allows us to format strings based on our requirements. We can provide the required format in three different ways.<\/p>\n<p>In the first method, we will use the <code>%<\/code> specifier. We can use this as a placeholder for different values. For example, the <code>%s<\/code> can act as a placeholder for a string variable, <code>%d<\/code> can act as a placeholder for an integer, and more.<\/p>\n<p>Let us see how we can use this to print variable and string in same line in Python.<\/p>\n<pre code = \"python\" title = \"Using the % formatting\">\na = 2\nprint(\"Java%dBlog\"%(a))    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nJava2Blog\n<\/div>\n<p>In the above example, we use the <code>%d<\/code> specifier as a placeholder for the variable value and add this in the string with the <code>%<\/code> formatting.<\/p>\n<h3><span id=\"Using_the_format_function\">Using the <code>format()<\/code> function<\/span><\/h3>\n<p>The <code>format()<\/code> function provides another convenient method to format strings. With this function, we can use the curly braces as a placeholder for variable values within the string. The required variables can be specified within the function.<\/p>\n<p>See the following example.<\/p>\n<pre code = \"python\" title = \"Using the format() function\">\na = 2\nprint(\"Java{}Blog\".format(a))\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nJava2Blog\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-print-without-newline\/\" target=\"_blank\" rel=\"noopener\">Print without Newline in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-print-without-newline\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/print-without-parentheses-python\/\" target=\"_blank\" rel=\"noopener\">Print without Parentheses in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/print-without-parentheses-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h3><span id=\"Using_the_fstrings\">Using the <code>fstrings<\/code><\/span><\/h3>\n<p>Python 3 introduced a new and efficient way to format strings using the <code>fstrings<\/code> feature. It provides an easy and efficient way to format strings by using the prefix <code>f<\/code> before the string.<\/p>\n<p>In this method also we will use the curly braces as placeholders within the string as we did in the previous method. <\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title = \"Using the fstrings\">\na = 2\nprint(f\"Java{a}Blog\")\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nJava2Blog\n<\/div>\n<h3><span id=\"Using_the_operator_and_str_function\">Using the <code>+<\/code> operator and <code>str()<\/code> function<\/span><\/h3>\n<p>The <code>+<\/code> operator functions differently for different types. For integer and float values, we use this to perform arithmetic addition. However, it can be used to concatenate strings when we use this operator with string-type values.<\/p>\n<p>The <code>str()<\/code> function in Python is used to convert a variable value to a string. We can use both these methods together to print variable and string in same line in Python.<\/p>\n<p>We will convert the variable to a string and concatenate it with the string using the <code>+<\/code> operator.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title = \"Using string concatenation\">\na = 2\nprint(\"Java\"+str(a)+\"Blog\")    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\nJava2Blog\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>To conclude, we discussed several methods to print variable and string in same line in Python. We can directly separate the two using a comma as discussed in the first method. The following methods highlight the use of string formatting to achieve the same.<\/p>\n<p>We discussed three formatting techniques. First, we used the <code>%<\/code> specifier where we specify missing variables separately and mention the <code>%<\/code> specifiers in their place in the string. The next two methods demonstrate the use of <code>format()<\/code> function and <code>fstrings<\/code> in Python, where we use the curly braces as a placeholder for missing variables.<\/p>\n<p>In the last method, we demonstrated the use of the <code>+<\/code> operator and <code>str()<\/code> function to concatenate strings with the required variables in Python to print them in the same line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsPrint Variable and String in Same Line in PythonUsing a commaUsing the % formattingUsing the format() functionUsing the fstringsUsing the + operator and str() functionConclusion &#x1f4a1; TL;DR To Print Variable and String in Same Line in Python, use print() method and separate String and variable with comma. a = 2 print(&#8220;Java&#8221;, a, &#8220;Blog&#8221;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[186,145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/21231"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=21231"}],"version-history":[{"count":5,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/21231\/revisions"}],"predecessor-version":[{"id":21294,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/21231\/revisions\/21294"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=21231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=21231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=21231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}