Python: Print a variable without spaces between values
Python Basic: Exercise-135 with Solution
Print Variable Without Spaces
Write a Python program to print a variable without spaces between values.
Sample value : x =30
Expected output : Value of x is "30"
Sample Solution-1:
Python Code:
# Assign the value 30 to the variable 'x'.
x = 30
# Use the 'format' method to create a formatted string that includes the value of 'x'.
# The curly braces {} are placeholders that will be replaced by the value of 'x'.
formatted_string = 'Value of x is "{}"'.format(x)
# Print the formatted string, which includes the value of 'x'.
print(formatted_string)
Sample Output:
Value of x is "30"
Sample Solution-2:
Python Code:
# Assign the value 30 to the variable 'x'.
x = 30
# Use the string formatting operator (%) to create a formatted string.
# The placeholder \"%i\" indicates that an integer value will be inserted at that location.
# Replace \"%i\" with the value of 'x' using the string formatting operator (%).
formatted_string = "Value of x is \"%i\"" % x
# Print the formatted string, which includes the value of 'x'.
print(formatted_string)
Sample Output:
Value of x is "30"
Sample Solution-3:
Python Code:
# Assign the value 30 to the variable 'x'.
x = 30
# Create a string by concatenating parts:
# 1. "Value of x is " - the static part of the string.
# 2. '\"' - a double quotation mark, escaped with backslashes, to include double quotes in the output.
# 3. The string representation of the variable 'x' using the 'str' function.
# 4. Another escaped double quotation mark '\"'.
# Print the final string that includes the value of 'x'.
print("Value of x is "+'\"' + str(x) + '\"')
Sample Output:
Value of x is "30"
Python Code Editor:
Previous: Write a Python program to input two integers in a single line.
Next: Write a Python program to find files and skip directories of a given directory.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/python-basic-exercise-135.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics