Regarding replace in string using .replace [duplicate] - python

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

Related

Python - Modify an input variable inside a function [duplicate]

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.

Why is the output different for both print statements? [duplicate]

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

String to Int in Python [duplicate]

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

Double-quotes instead of single ones [duplicate]

This question already has answers here:
Single quotes vs. double quotes in Python [closed]
(19 answers)
double quotes in string representation
(1 answer)
Closed 6 years ago.
Why isn't the output of x single-quoted like y? My speculation so far is that because there's a single-quote inside x it makes it awkward if it would be printed with single-quotes. How can I prevent that from happening? Does it have to do something with escape sequences?
x = "-Why's that?"
y = "-What are you talking about dude?"
print ("%r\n%r")% (x, y)
Output:
"-Why's that?"
'-What are you talking about dude?'

How to convert int format to string format when defining python programs [duplicate]

This question already has answers here:
Convert integer to string in Python
(14 answers)
Closed 7 years ago.
I want to convert my input from int to string but i cannot do it! This is my code! Please help!
def hi(x):
print x
if I put a letter for x,an error message comes! I don't want to give my input within double quotation marks.without doing that, is there any way?
Its quiet simple.
def hi(intx):
target = ''.format(intx)
print target
def hi(x):
print(type(x))
x= str(x)
print(type(x))
So your argument is an integer but when you do str(x), you change the data type to string.

Categories