This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 5 years ago.
I'm new to python and I was wondering how to put a variable inside a string to show the contents of such variable in an LCD 20x4 display (2004A).
The code I have looks as follows:
with open("temperature.txt") as myfile:
currenttemp=myfile.readlines()
lcd_string("%(currenttemp)" ,LCD_LINE_1,2)
Unfortunately, this prints "%(currenttemp)" in the LCD display, and not the currenttemp variable value.
An example of string formatting in Python:
Code:
a = "one line file"
print("this file contains: %s" %a)
Output:
this file contains: one line file
For performance comparison of various string formatting technique see here: Python string formatting: % vs. .format
Related
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have a text file that looks like this:
../../../../foo/bar
../../this/that
../barfoo
and I want:
foo/bar
this/that
barfoo
with open('file_list.txt', 'r') as file_list:
for file_list_lines in file_list:
file_list_lines.lstrip('../')
print(file_list_lines)
I tried .lstrip('../') but nothing was stripped from the beginning of the line.
The string.lstrip() does not do the string manipulation in place. In other words, you would need to store it into a variable like so:
stripped_line = file_list_lines.lstrip('../')
print( stripped_line )
In your version, you did the lstrip, but did not store the result of that operation anywhere.
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}
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 4 years ago.
I am using a Markov chain. When the chain arrives at a particular state, two files (a .png and an .mp3) need to open.
s is the current state of the chain, an integer from 1-59.
I can't seem to find how to open the file with the same number as 's'.
I'm sure it has something to do with %str formatting, but I can't seem to implement it.
img = Image.open('/.../.../s.png')
img.show()
You should use the following line in your code:
img = Image.open('/.../.../{0}.png'.format(s))
You can format a string using a variable like this
>>> s = 10
>>> '/path/to/file/{}.png'.format(s)
'/path/to/file/10.png'
This question already has an answer here:
Int conversion not working [duplicate]
(1 answer)
Closed 6 years ago.
Is there some python way to convert a text (from a file for example) into a format string?
I mean for a text file:
this is a {format}
string.
Load it in python and have it become like the triple quotes format string:
var = """this is a {format}
string."""
I know how to just read the file and replace the curly braces, but I was wondering if there is already something that does this.
Thanks
Edit:
This is the code I've tried:
with open('file.txt', 'r') as rs:
lines = rs.readlines()
text = ','.join(lines)
print(text)
text.format(format='something_else')
print(text)
It just prints the text file.
I'm looking to know if there is a more pythonic way then me having to write a class that does this.
Thanks
variable.format() returns the formatted text and does not change the content of variable. Try
print(text.format(format="bla"))
or
text = text.format(format="bla")
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 7 years ago.
How to convert a=b=2 string into a=2, b=2 (actual assignments) in python?
There are some python parsers which give output for expressions like 2+3+4.
But how to use a=b=2 string into variable as a=2 b=2?
s = 'a=b=2'
exec(s) #executes the string as a python command
And you are done
print(a)
2
print(b)
2
Refer to this discussion for more info:
How do I execute a string containing Python code in Python?