This question already has answers here:
Python string literals - including single quote as well as double quotes in string
(3 answers)
Using quotation marks inside quotation marks
(12 answers)
Closed 3 years ago.
I'm having trouble figuring out how to print a very specific line.
I tried using .format and I also tried the print("str",variable,"str") method but I can't seem to figure out how to make it print correctly.
feet=5
inches=6
print('Room Length:{}' {}"'.format(feet,inches))
I want the computer to print; Room Length: 5' 6"
but I am not sure how to print this while keeping the apostrophe and the quotations for feet and inches.
print('Room Length:{}\' {}\"'.format(feet,inches))
Related
This question already has answers here:
Python 3: receive user input including newline characters
(2 answers)
Process escape sequences in a string in Python
(8 answers)
Closed 9 months ago.
I am looking to type from the Python Console in Pycharm to respond to a call from input(). I would like to somehow get a newline into this string, but when I try to literally type "\n", input() automatically escapes the escape backslash into something like result = "\n" so that what I typed exactly will be printed out if I use print(result).
Demonstration of problem
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
In class im meant to write a program that arranges coordinates for you. I wrote this:
x = input("")
y = input("")
z = input("")
print("(",x,",",y,",",z,")\n)
and the output is: (␣0␣,␣-7831␣,␣2323␣)⤶
how do I stop the extra spaces from appearing so I get this?: (0,␣-7831,␣2323)⤶
In modern Python, the nicest way is to use an f-string:
print(f"({x},{y},{z})")
Note how the string is prefixed with f. Everything between the curly braces {} then gets interpreted as a Python expression which is subsequently converted to a string and inserted at that point.
Note that print already follows up with a newline, so unless you want an extra one (that is, a blank line), you don't need to add \n yourself.
This question already has answers here:
Understanding difference between Double Quote and Single Quote with __repr__()
(3 answers)
Closed 2 years ago.
Why does the Python console returns single quotes for string literals for all types of quote delimeters?
>> '1'
'1'
>> "1"
'1'
>>"""1"""
'1'
Python makes it convenient for itself to handle objects in a simple manner. If you really need an explicit way of memorizing quotes use literal quote characters.
>>> "\"\"Look, I'm around two qoutes!!!\"\""
'""Look, I\'m around two double qoutes!!!""'
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
If I have a string like test string. something \n else how would I split the string on both . and \n characters? I have tried using re.split(". \n", text) but it doesn't seem to be working.
Figured it out - I needed to use re.split("\\.|\n", text).
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
how can you format a string of this form in Python 3?
'''{name}{{name}}'''.format(name="bob")
the desired output is: bob{bob}, but the above gives: bob{name}.
one solution is to add another argument to format:
'''{name1}{name2}'''.format(name1="bob", name2="{bob}")
but this is excessive. is there a way to properly escape { such that inner {x} can still be interpolated and one can only pass a single name to format?
Add one more level of {}:
'''{name}{{{name}}}'''.format(name="bob")
which outputs:
bob{bob}