Replacing multiple strings in Python [duplicate] - python

This question already has answers here:
How to replace multiple substrings of a string?
(28 answers)
Closed 4 years ago.
I currently have the below Python code which works well:-
caseURL = r"\\mydomain\abc\lp_t\GB\123456\Original Format"
caseURL = caseURL.replace("lp_t", "lp_i")
caseURL = caseURL.replace("Original Format", "1")
This works fine as said and carries out the below conversion:-
\\mydomain\abc\lp_t\GB\123456\Original Format
\\mydomain\abc\lp_i\GB\123456\1\
This however just seems a bit clumsy. Is there a more pythonesque way to perform these two segment replacements?
Thanks

A similar post already exists:
How to replace multiple substrings of a string?
You can pick one answer from multiple options in the above post.

Related

python split regex into multiple lines [duplicate]

This question already has answers here:
How to split long regular expression rules to multiple lines in Python
(6 answers)
Closed 4 years ago.
Just a simple question. Lets say i have a very long regex.
regex = "(foo|foo|foo|foo|bar|bar|bar)"
Now i want to split this regex into multiple lines. I tried
regex = "(foo|foo|foo|foo|\
bar|bar|bar)"
but this doesnt seems to work. I get different outputs. Any ideas?
Just do it like this
regex = "(foo|foo|foo|foo" \
"|bar|bar|bar)"

How do I turn a list into a string [duplicate]

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'])

Why is this split method not working correctly? [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t

pythonic way to write the code for search and replace string [duplicate]

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")

How to print bold text in python with ```format```? [duplicate]

This question already has answers here:
How can I print bold text in Python?
(17 answers)
Closed 6 years ago.
I'm aware of the this solution and this solution when using string concatenator +. However I couldn't find how to do it with the new printing style (more details here), e.g. print '{:10s}'.format(str).
The same ANSI escape sequences work just fine.
print('\033[1m{:10s}\033[0m'.format('foo'))

Categories

Resources