This question already has answers here:
Why doesn't .strip() remove whitespaces? [duplicate]
(3 answers)
Closed 1 year ago.
I want to learn what is the difference between two code lines.I couldn't find the difference.Whenever I try to run the second code,it doesn't affect string a.
Could someone tell me why the second code line doens't work?
a = "aaaaIstanbulaaaa".strip('a') #Affects the string
print(a)
>>>Istanbul
a = "aaaaIstanbulaaaa" #Doesn't affect the string
a.strip('a')
print(a)
>>>aaaaIstanbulaaaa
str.strip returns a value; it does not modify the str value invoking it. str values are immutable; you cannot modify an existing str value in any way.
The str.strip() isn't an inplace method, it doesn't change the current object you're calling with, it returns a new one modified
That is an example of inplace modification
x = [1, 2, 3]
x.append(4)
# x is [1, 2, 3, 4]
Related
This question already has answers here:
python: why does replace not work?
(2 answers)
Closed 1 year ago.
a = 'dog'
a.replace('dog', 'cat')
print (a)
Really basic question, the function seems to be fairly straightforward but it just isn't replacing in this instance for some reason - is it because replace doesn't inherently change "a"?
Yes, you are correct. It won’t modify a.
Replace function will return a replaced string.
So, if you like to replace the text in a. Use the below code.
a = 'dog'
a = a.replace('dog', 'cat')
print (a)
Strings are immutable data types in Python which means that its value cannot be updated. Variables can point at whatever they want.
str.replace() creates a copy of string with replacements applied. See documentation.
Need to assign a new variable for the replacement.
a = 'dog'
b = a.replace('dog', 'cat')
print(b)
Output:
cat
This question already has answers here:
What is the __lt__ actually doing for lists [duplicate]
(2 answers)
Closed 4 years ago.
How come max([1,2,3], [1,1,4]) returns [1,2,3] not [1,1,4]?
I was asked this question in a class. I don't understand why it returns [1,2,3] and the logic behind it (even if it returns [1,1,4], I still don't understand what max() function does).
The function max will succeed in outputing a maximum as long as the provided arguments are comparable.
In this case, the first argument is greater than the second with regard to list-ordering.
>>> [1, 2, 3] > [1, 1, 4]
True
This question already has an answer here:
python append list return none
(1 answer)
Closed 5 years ago.
The following works just fine:
b = [1,]
b.append([2, 3])
# returns type "list" [1, 2, 3]
But the following doesn't:
[1,].append([2, 3])
# returns type "NoneType"
This holds true for a few of the list methods that' I've tried. Why does Python require explicit variable declaration before applying a method?
You're not reading that right. .append() returns None in both cases. In the first case, it is b that results in [1, 2, 3]. In the second, that list is created also, but you don't have access to it becaues you didn't assign a variable name to it.
append returns None as its value, but alters the list. Try
b = [1,]
output = b.append([2, 3])
You'll get None for output, too.
This question already has answers here:
Converting a string representation of a list into an actual list object [duplicate]
(3 answers)
Closed 8 years ago.
I am pretty confused here.
I have a function that has a list as an argument. It then does list-specific functions on it. Here is an example:
def getValues( my_list ):
for q in my_list:
print(q)
But, I get my list from USER INPUT like this:
a_list = input("Please enter a list. ")
getValues(a_list)
The built-in input() function returns a string, not a list. My solution is to take that string of the list and turn it back into a list.
Only I don't know how.
Thanks!
Its what that ast.literal_eval is for :
>>> ast.literal_eval('[1,2,3]')
[1, 2, 3]
This question already has answers here:
How to find the name of a variable that was passed to a function?
(3 answers)
Closed 8 years ago.
Can a Python (3) function "know" the literal value of a parameter passed to it?
In the following example, I want the function listProcessor to be able to print the name of the list passed to it:
list01 = [1, 3, 5]
list02 = [2, 4, 6]
def listProcessor(listName):
"""
Function begins by printing the literal value of the name of the list passed to it
"""
listProcessor(list01) # listProcessor prints "list01", then operates on the list.
listProcessor(list02) # listProcessor prints "list02", then operates on the list.
listProcessor(anyListName) # listProcessor prints "anyListName", et cetera…
I've only recently resumed coding (Python 3). So far everything I've tried "interprets" the parameter and prints the list rather than its name. So I suspect I'm overlooking some very simple way to "capture" the literal value of parameters passed to a Python function.
Also, in this example I've used a list's name as the parameter but really want to understand how to capture any type of parameter's literal value.
while there is something called introspection, which can be used to obtain names of variables in some cases, you're probably looking for a different data structure. if you put your data in a dict, you can have the "labels" in the keys and the "lists" in the values:
d = { "list01": [1, 3, 5],
"list02": [2, 4, 6] }
def listProcessor(data, key):
print key
print data[key]
listProcessor(d, "list01")