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?
Related
This question already has answers here:
How can I do a dictionary format with f-string in Python 3 .6?
(7 answers)
Closed 11 months ago.
How can I convert this to an f-string?
message = '{status} : {method} : {url}'.format(**call_components)
str.format() is usually fine, but if you really want an f-string then it would look like this:
message = f'{call_components["status"]} : {call_components["method"]} : {call_components["url"]}'
This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I cant seem to write something like : "C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
As a string to print or otherwise, i think its something to do with the slashes
There can be 3 ways :
1) Either use r (raw strings) before the actual string :
For example : r"C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
2) Or use // like:
For example : "C:\\Users\\alon4\\Desktop\\Learning Material\\Odyssey\\Lab a2\\Magnetism\data"
3) Use slashes: "C:/Users/alon4/Desktop/Learning Material/Odyssey/Lab a2/Magnetism/data"
(As suggested by Óscar López)
This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Add new statements to Python without customizing the compiler
(2 answers)
Closed 4 years ago.
Can I create a statement in python 2.7?
Something similar to the print statement, that it's like a function, but gets the parameters without parenthesis.
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