This question already has answers here:
How do I convert a list into a string with spaces in Python?
(6 answers)
Closed 2 years ago.
As I join 2 strings like:
print("not" + "X")
The result is like this:
notX
How could I make this into:
not X
Try using:
print("not","X")
# or:
' '.join(i for i in ['not','X'])
# or:
' '.join(('not','X'))
instead of:
print("not"+"X")
Related
This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')
This question already has answers here:
Find and replace string values in list [duplicate]
(6 answers)
How to change the list separator in python?
(2 answers)
Replacing instances of a character in a string
(17 answers)
Closed 2 years ago.
I have a list like this ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL'] and I want to convert it to ['BASE', 'BASE_xBU_xPY', 'BU_GROU_ REL', 'PY_REL'] in short convert space to underscore for value in b/w quotes
I would suggest using replace along with map
Example:
my_list = ['BASE', 'BASE xBU xPY', 'BU GROUP REL', 'PY REL']
converter = lambda x: x.replace(' ', '_')
my_list = list(map(converter, my_list))
my_list
['BASE', 'BASE_xBU_xPY', 'BU_GROUP_REL', 'PY_REL']
This question already has answers here:
Split string based on regex
(3 answers)
Closed 2 years ago.
I'm try to split text from string, but I can't.
I have 1;2 | 2;4, for example in my string, and I want to:
['1;2', '|', '2;4']
You can use str.partition:
>>> '1;2 | 2;4'.partition("|")
('1;2 ', '|', ' 2;4')
This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Remove specific characters from a string in Python
(26 answers)
Closed 3 years ago.
Given this string, ###hi##python##python###, how can I remove all instances of '#'?
v = "###hi#python#python###"
x = v.strip("#")
print(x)
Expected output: "hipythonpython"
Just use replace
the_str = '###hi##python##python###'
clean_str = the_str.replace('#','')
print(clean_str)
Output
hipythonpython
What you want is not strip, but replace:
v = '###hi#python#python###'
x = v.replace('#', '')
print(x)
Output:
hipythonpython
This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 3 years ago.
I have this string:
var(HELLO,)|var(Hello again)| var(HOW ARE YOU?)|outV(0)|outV(1)|outV(2)|END
I want to split it on the |. I don't want it to split at the white space, only at the |.
Is this possible?
The way to do this is clearly documented here.
Example:
>>> myString = "subString1|substring2|subString3"
>>> myString = myString.split("|")
>>> print myString
["subString1", "subString2", "subString3"]