This question already has answers here:
Check if string contains only whitespace
(11 answers)
Closed 4 years ago.
I have a variable:
exchange_name = ''
Now I want to perform some operation based on checking if it is equal to an empty string.
So, I do:
if exchange_name == '':
# perform some operation
But how can I generalize the solution so that exchange_name can contain any number of spaces, e.g.:
exchange_name = ' '
or
exchange_name = ' '
Can anybody suggest an approach? Thanks in advance.
exchange_name.strip()==''
strip removes all empty spaces.
Try to use rstrip to remove spaces from begin and end of string.
if mytext.rstrip() == '':
do_it()
Related
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 months ago.
So, I've seen a lot of answers for removing duplicate characters in strings, but I'm not trying to remove all duplicates - just the ones that are beside each other.
This is probably a lot more simple than what I'm doing, but this is what I've been attempting to do (and failing miserably at)
for j in range(2, len(string)-1):
char = string[j]
plus = string[j+1]
minus = string[j-1]
if char == plus or char == minus:
string.replace(char, "")
For reference, the code SHOULD act as:
input: ppmpvvpmmp
output: pmpvmp
But instead, the output does not change at all.
Again, I'm aware that this is most likely very easy and I'm overcomplicating, but I'm genuinely struggling here and have tried a lot of similar variations
I would use a regular expression replacement here:
inp = "ppmpvvpmmp"
output = re.sub(r'(\w)\1', r'\1', inp)
print(output) # pmpvpmp
The above assumes that a duplicate is limited to a single pair of same letters. If instead you want to reduce 3 or more, then use:
inp = "ppmpvvvvvpmmmp"
output = re.sub(r'(\w)\1+', r'\1', inp)
print(output) # pmpvpmp
This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 2 years ago.
So I have a function that takes a string, iterates over a set of characters and returns a string with a backslash added to all the occurences of any character in that particular string:
def re_escape(string):
res = "|\^&+\-%*/=!>"
for i in res:
if i in string:
a = string.split(i)
adjusted = ""
for y in a:
adjusted+="\\"+i+y
adjusted = adjusted[2:]
string = adjusted
print(string)
return string
Giving this function the string " <br>" returns " <br\>", as desired.
However, going back to the part of the program calling this function and receiving the string as a return value, trying to print it results in " <br\\>" being printed. Is there any way to prevent it from adding the second undesired backslash?
Give it a try: string.replace('\\\\','\\').
This question already has answers here:
Remove all whitespace in a string
(14 answers)
Python String replace doesn't work [duplicate]
(1 answer)
Closed 1 year ago.
I have a function that begins like this:
def solve_eq(string1):
string1.strip(' ')
return string1
I'm inputting the string '1 + 2 * 3 ** 4' but the return statement is not stripping the spaces at all and I can't figure out why. I've even tried .replace() with no luck.
strip does not remove whitespace everywhere, only at the beginning and end. Try this:
def solve_eq(string1):
return string1.replace(' ', '')
This can also be achieved using regex:
import re
a_string = re.sub(' +', '', a_string)
strip doesn't change the original string since strings are immutable. Also, instead of string1.strip(' '), use string1.replace(' ', '') and set a return value to the new string or just return it.
Option 1:
def solve_eq(string1):
string1 = string1.replace(' ', '')
return string1
Option 2:
def solve_eq(string1):
return string1.replace(' ', '')
strip returns the stripped string; it does not modify the original string.
This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 6 years ago.
if I have a str and the I use str.index(char) and the char does not exist, is it possible with one line to assign another char to the variable?
Maybe like this
str = "bar/foo"
t = str.index("_") | "Empty"
str dosen't contain _ , so the string "Empty" should be assigned instead.
Since str.index() would throw a ValueError if substring is not in a string, wrap it into an if/else checking the presence of substring in a string via in operator:
>>> s = "bar/foo"
>>> s.index("_") if "_" in s else "Empty"
"Empty"
>>> s = "bar_foo"
>>> s.index("_") if "_" in s else "Empty"
3
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 6 years ago.
For some reason string.replace(" ", "") is not working.
Is there another way of removing white spaces from a string?
Why is .replace not working in this particular situation?
string = input('enter a string to see if its a palindrome: ')
string.replace(' ', '') # for some reason this is not working
# not sure why program will only work with no spaces
foo = []
bar = []
print(string)
for c in string:
foo.append(c)
bar.append(c)
bar.reverse()
if foo == bar:
print('the sentence you entered is a palindrome')
else:
print('the sentence you entered is not a palindrome')
replace() returns a new string, it doesn't modify the original. Try this instead:
string = string.replace(" ", "")