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.
I have a string S = 'spam'
When I use the method replace as S.replace('pa', 'xx')
S.replace('pa', 'xx')
The output I get is -
Out[1044]: "sxxm's"
Why then are the python strings known to be immutable ?
S = 'spam'
S.replace('pa', 'xx')
print S
You will get the same string 'spam'
You are not saving the return value.
Snew = S.replace('pa', 'xx')
should work
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)
Why can a function modify some arguments as perceived by the caller, but not others?
(13 answers)
Closed 3 years ago.
The following code:
def function(X):
X.upper()
if X == 'YES':
print ('success')
else:
print ('fail')
function('yes')
Produces:
fail
But this code:
def function2(X):
Y = X.upper()
if Y == 'YES':
print ('success')
else:
print ('fail')
function2('yes')
Gives me:
success
Why is this? I want to be able to edit my input variables within my functions. Is there a more efficient way to do this than copying variable values to new variables? I'm running Python 3.7.1.
Thanks!
Because "".upper() returns new string, it doesn't change the original. Strings are immutable in Python.
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 4 years ago.
str1="This is a Case "
print(str1.swapcase())
print(str1)
I expect the output for both print statements to be swapped as:
"tHIS IS A cASE" but the output for first print is "tHIS IS A cASE" and second print is the original str1.
swapcase doesn't change the string that you call it on, it returns a new string. If you want to change the original string, you have to reassign it with the returned value.
str1 = str1.swapcase()
This question already has answers here:
replace characters not working in python [duplicate]
(3 answers)
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 4 years ago.
I have tried to use .replace in python to replace an empty list in a string but it is not working. Could anyone please tell me how?
x = ['check-[]|man', 'check-[]|king']
for y in x:
if "[]" in y:
y.replace("[]", "o")
print(y)
The results gave me this despite using .replace:
check-[]|man
check-[]|king
y.replace returns a value.
You have to assign it back
y = y.replace("[]", "o")
You need to assign i back to variable y:
x = ['check-[]|man', 'check-[]|king']
for y in x:
if "[]" in y:
y=y.replace("[]", "o")
print(y)
Output:
check-o|man
check-o|king
This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s
This question already has answers here:
In the Python interpreter, how do you return a value without single quotes around it?
(2 answers)
Closed 8 years ago.
In [4]: {'a': "hello"}['a']
Out[4]: 'hello'
I want the output to instead be:
Out[4]: hello
What's the most elegant way to achieve this?
I am also using repr().rjust() later on to print a list of dictionaries into a neatly formatted table, and I notice the repr function also adds quotes which is annoying:
def print_dictionary(d): print repr(d['a']).rjust(10)
print_dictionary({'a':'hello'})
yields:
'hello'
when I want:
hello
Just drop the repr:
def print_dictionary(d): print (d['a']).rjust(10)