Strings in python
A string in Python is a sequence of characters. In some sense strings are similar to lists, however,
there are important differences. One major difference is that Python strings are immutable, mean-
ing that we are not allowed to change individual parts of them as we could for a list. So if x is an
existing string, then x[i] gets the character at position i, but we are not allowed to reassign that
character, as in x[5] = ’s’.
>>> x = ’ g o b b l e t y g o o k ’
>>> x [2]
’b ’
>>> x [5]
’e ’
>>> x [5] = ’s ’
T r a c e b a c k ( most recent call last ):
File " < stdin > " , line 1 , in < module >
T y p e E r r o r : ’ str ’ object does not s u p p o r t item a s s i g n m e n t
Just as for lists, string items are indexed starting at 0. Slicing for strings works exactly the same as
for lists. The length function len is the same as for lists, and concatenation is the same too. But
19the list methods append, insert, delete, and pop are not available for strings, because strings
are immutable. If you need to change an existing string, you must make a new, changed, one.
There are many string methods for manipulating strings, documented in the Python Library
Reference Manual [2]. For example, you can capitalize an existing string x using x.capitalize();
this returns a new copy of the string in which the first character has been capitalized.
>>> a = ’ g o b b l e t y g o o k is r e f r e s h i n g ’
>>> a . c a p i t a l i z e ()
’ G o b b l e t y g o o k is r e f r e s h i n g ’
Other useful methods are find and index, which are used to find the first occurence of a substring
in a given string. See the manuals for details.
there are important differences. One major difference is that Python strings are immutable, mean-
ing that we are not allowed to change individual parts of them as we could for a list. So if x is an
existing string, then x[i] gets the character at position i, but we are not allowed to reassign that
character, as in x[5] = ’s’.
>>> x = ’ g o b b l e t y g o o k ’
>>> x [2]
’b ’
>>> x [5]
’e ’
>>> x [5] = ’s ’
T r a c e b a c k ( most recent call last ):
File " < stdin > " , line 1 , in < module >
T y p e E r r o r : ’ str ’ object does not s u p p o r t item a s s i g n m e n t
Just as for lists, string items are indexed starting at 0. Slicing for strings works exactly the same as
for lists. The length function len is the same as for lists, and concatenation is the same too. But
19the list methods append, insert, delete, and pop are not available for strings, because strings
are immutable. If you need to change an existing string, you must make a new, changed, one.
There are many string methods for manipulating strings, documented in the Python Library
Reference Manual [2]. For example, you can capitalize an existing string x using x.capitalize();
this returns a new copy of the string in which the first character has been capitalized.
>>> a = ’ g o b b l e t y g o o k is r e f r e s h i n g ’
>>> a . c a p i t a l i z e ()
’ G o b b l e t y g o o k is r e f r e s h i n g ’
Other useful methods are find and index, which are used to find the first occurence of a substring
in a given string. See the manuals for details.
Comments
Post a Comment