184.108.40.206 apply functions and string processing methods
Python. Strings (Строки)
We already saw the Set collection in the previous lesson, which can store more than one value. Today we are getting acquainted with the second collection - String. And although we have often used the string type, this is the first time we will consider a string as a collection of symbols.
We get the string when we enclose characters in quotation marks, for example, "text", when we use input() when we use the conversion function str ().
We can already measure the length of a string using the len() function and determine the occurrence of one string within another using the in operation.
Unlike sets, which contain unordered elements, the sequence of characters in a string has its own order, and each character has its own address in the string - an index by which it can be accessed.
You can refer to each character by its index. To do this, use the following entry:
s - refer to the character with index 1 in string s.
Unlike many languages, Python also uses negative indices.
When accessing an index that does not exist, we will get an error.
If we want to correct one letter in a word, for example,
The Python interpreter throws an error - it means that it is impossible to change a single character of the string, that is, the string is an immutable data type in Python.
Iterating over the elements of a string.
Since a string is a collection that consists of character elements, we can iterate over each element of the string as we iterate over sets. To iterate over, we will use a for a loop.
String data structure features:
Task. Determine how many vowels are in the line.
But, since the characters in the string are numbered, we have another way to iterate over all the elements in the string: iterate over all indices using the already familiar for i in range (...) construction.
|Просмотров: 388 | ||
|Всего комментариев: 0|