+ 2
Capitalize in python
Why does the attached code work properly? Strings are immutable, so why does capitalize work though? https://sololearn.com/compiler-playground/cOKudjn5shxJ/?ref=app
10 Answers
+ 7
word = "sunrise"
print(id(word))
word = word.capitalize()
print(id(word))
#Sample output:
#140532121532720
#140532121533040
#Change in id indicates different strings
+ 6
The string methods return new values and dont change the original string.
+ 4
When 'capitalize' is called on a string in Python, it returns a new string with the first letter capitalized, while leaving the original string unchanged. This works by creating a new string object with the first letter capitalized without modifying the original string. It's like taking a picture of the original string, tweaking the picture, and showing that to you while leaving the original string as it is.
+ 4
Anuska Bhattacharyya ,
word = ["Sample"]
word[0] = s # NameError: name 's' is not defined
print(word)
The NameError is because you forgot to put quotes around s, so the interpreter thought it was a name, but that name was never defined.
Even if you put quotes around it, you're not assigning to a slice of word but to an indexed item of word (its only item), and the list will mutate from ["Sample"] to ['s'].
word = ["Sample"]
word[0] = "s"
print(word) # ['s']
Thanks alot for telling me.
+ 3
So it's like I said before. The variable 'word' is overwritten with a new value. So actually it's a complete new string value.
+ 3
"""
Bilbao ,
Here's another variation on your example.
This shows that you can immediately use in the same statement the new unnamed str objects returned by some methods of the str class when called on a name that refers to an existing str object without needing to assign any of them to that name.
Being unnamed, those new str objects become candidates for implicit deletion by automatic garbage collection when the statement ends.
Some other str methods return int objects.
The str object referred to by the name is unaffected.
reference:
https://docs.python.org/3/library/stdtypes.html#string-methods
"""
word = "sunrise"
print(word.capitalize()) # Sunrise
print(word.upper()) # SUNRISE
print(word.replace("rise", "set")) # sunset
print(word.count("s")) # 2
print(word.find("s")) # 0
print(word) # sunrise
+ 1
Anuska Bhattacharyya ,
I think you misunderstand some things.
You seem to be saying that "s" and "S" have the same value but different cases. However, those are two different characters with two different values. It doesn't matter that they are related alphabetically.
Also in your example code involving a list:
word = ["Sample"]
word[0] = s # NameError: name 's' is not defined
print(word)
The reason you get a NameError has nothing to do with slicing. It's because you forgot to put quotes around s, so the interpreter thought it was a name, but that name was never defined.
Even if you were to put quotes around "s", you would not be assigning to a slice of word, as you implied, but to an indexed item of word (its only item), which would mutate word from ["Sample"] to ['s'].
word = ["Sample"]
word[0] = "s"
print(word) # ['s']
0
I guess it's because the original variable is overwritten by itself.
0
Here is the code to capitalize the whole word
word = "sunrise"
word = word.upper()
print(word)