Addressing part of a string through array syntax in Python

Is it possible in Python to address a specific character in a string with the syntax of a standard array?

Example: PHP:

$foo = 'bar';
echo $foo[1]; // Output: a

This does not work, as in PHP, so I wanted to know if this is possible using another method?

+3
source share
2 answers

As Adam pointed out, reading from an array of strings is possible in Python using indexing syntax. Impossible, although written in a string using this syntax:

>>> s = 'bar'
>>> s[2] = 'z'
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Perhaps this was the problem you encountered?

+5
source

, . , , PHP. , Python $.

foo = 'bar'
print foo[1] # Output: a

Python, . , Python.

+1

Source: https://habr.com/ru/post/1730086/


All Articles