How to use strip in the middle of a string [duplicate] - python

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

Related

How to delete certain words from string without spaces? [duplicate]

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

how to convert space to underscore in python list [duplicate]

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

How can I give spaces while joining 2 strings together? [duplicate]

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

Replacing all characters of a given string with a specific single character python with no loops [duplicate]

This question already has answers here:
replace all characters in a string with asterisks
(4 answers)
Closed 4 years ago.
str0 = input("Enter str: ")
str0 = str0.replace(str0, '_')
print(str0)
I need to replace each one of the characters without using loops or conditions
str0 = input("Enter str: ")
str0 = '_'*len(str0)
print(str0)
Should work. The multiplication puts the right amount of _'s so that all of them are replaced.

Python - how to remove commas, square brackets and quotation marks from list [duplicate]

This question already has answers here:
Pythonic way to print list items
(13 answers)
Closed 7 years ago.
How do I remove the quote marks and commas and brackets from this result:
encrypt = input("enter your string: ")
encrypt = encrypt.replace(" ","")
encrypt_list = [encrypt[i:i+5] for i in range(0, len(encrypt), 5)]
print (encrypt)
print (encrypt_list)
If the input was: 5 blocks of text test
The output is: ['5bloc', 'ksoft', 'extte', 'st']
I need it to be: 5bloc ksoft extte st
You can use str.join, like this:
>>> s = ' '.join(['5bloc', 'ksoft', 'extte', 'st'])
>>> print(s)
5bloc ksoft extte st

Categories