This question already has answers here:
Pythonic way to replace chars
(2 answers)
Closed 1 year ago.
I have a problem when trying to switch the signs of all arithmetic operations inside a string in Python.
My input looks like this: "+1-2"
And the output should be something like that: "-1+2"
But when I try to replace the characters with replace() function:
"+1-2".replace("+", "-").replace("-", "+")
The output I get is that: '+1+2'
Looks like the replace functions are replacing everything at the same time, so it will never switch it correctly in that way. A one-liner solution with similar functions would be very helpful.
Use str.translate:
s = "+1-2+3+4-2-1"
t = str.maketrans('+-','-+')
print(s.translate(t))
Output:
-1+2-3-4+2+1
Related
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has an answer here:
How can I find all matches to a regular expression in Python?
(1 answer)
Closed 3 years ago.
I'm currently practicing regex. I declared--> str1="bbccaa". I want result to be all the b's and a's i.e 'bbaa'. I tried-> '[^c]+' ,[ab]+ But everything I tried ultimately gave an output as 'bb'. Can someone tell me where I'm going wrong and also the solution, please??
Try this:
import re
s = "bbccaa"
print(re.sub("[^ab]+", r"", s))
#bbaa
I would use re.findall for that, then join results following way:
import re
str1="bbccaa"
output = ''.join(re.findall('a|b',str1))
print(output)
Output:
bbaa
I do not see way to make it solely with re (without join)
Since we are practicing here, another expression, not the best one, would be:
([ab]+)|(.+?)
Demo
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
Search and replace operation
(2 answers)
Closed 6 years ago.
I have written code to search and replace string in make command as per user input
make language='english' id=234 version=V1 path='/bin'
In above code i searched version=V1 and replace version with version=V2
import re
strings = "make language='english' id=234 version=V1 path='/bin'"
search_pattern= re.search('version=(.*?)\s', strings)
old_str = search_pattern.group(1)
print test.replace(old_str, "V2")
Can anyone help me write above code in pythonic way or any other way to write above code
It's very easy if you use str.replace
String = "make language='english' id=234 version=V1 path='/bin'"
String = String.replace("version=V1", "version=V2")
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
It's really odd but I have this string:
"['please', 'help']"
I want something that would get one argument at a time.
I've searched everywhere for this but I didn't find anything.
Thanks in advance
While eval is a correct approach, it can often have negative consequences. I suggest using ast.literal_eval, which is a more safe approach (as mentioned by the linked docs):
import ast
s = "['please', 'help']"
s_list = ast.literal_eval(s)
print s_list
Are you looking for something like this?
string = "['please', 'help']"
string_list = eval(string)
print string_list[0], string_list[1]
Edit: you should ideally use ast.literal_eval as the other answer suggests, if you are unsure of what the string contains.