Python Language String: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:




The <code>[]</code> operator can be used to read strings from the sequence, but not modify the sequence. Because strings are immutable, an attempt to change a character at a specific position in string will throw an <code>TypeError</code> exception:
<span id='String_Immutability'></span>The <code>[]</code> operator can be used to read strings from the sequence, but not modify the sequence. Because strings are immutable, an attempt to change a character at a specific position in string will throw an <code>TypeError</code> exception:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
s = 'abc'
s = 'abc'

Revision as of 19:07, 18 June 2022

Internal

Overview

String are a Python sequence of characters. Strings are immutable, a string cannot be changed in-place. Python 3 supports the Unicode standard, so Python 3 strings can contain characters from any written language in the world.

The [] Operator and String Slices

The [] operator can be used to read strings from the sequence, but not modify the sequence. Because strings are immutable, an attempt to change a character at a specific position in string will throw an TypeError exception:

s = 'abc'
s[0] = 'x'
[...]
TypeError: 'str' object does not support item assignment

TO DEPLETE

Python Language String TODELETE