Lines. Overview

A string - is a list (or sequence) of characters located in a particular okay. The whole sequence of characters is treated as a single object.

A character is anything you can type on the keyboard with a single keystroke (letter, number, backslash, or some other character).

Strings can have spaces: "Hello world!".

An empty string is a string that has 0 characters.

Python accepts as strings anything enclosed in quotes (" ") or apostrophes (' ').

In Python, a string is of type str.
 
Need to remember!
Strings in Python are immutable!


You can write a new value to a string using the input statement
s = input()

You can also simply assign a string value to the variable, such as  
s = 'Python'
or so
s = "Python"

You can determine the length of a string using the built-in function len()
n = len(s)


String comparison

Strings can be compared to each other just like numbers. You can determine which of the lines is greater, which is less.

When comparing strings, character codes are compared. There are dozens, if not hundreds, of character encodings. The easiest way to understand this concept is to understand one of the simplest, ASCII (read here).  ;

It is necessary to understand that in modern encodings both Russian and English letters are arranged in alphabetical order, the numbers also go from smaller to larger. 
For example, in the ASCII code table, the code for the English letter 'A' - 65, letters 'a' - 97, digit '0' has code 48. Russian letters are located in the extended part of the ASCII code table (numbers from 128 to 255). Capital letters come before (i.e. have a smaller code) than lowercase letters.

In most of the encoding tables used, the patterns are the same, lowercase letters are later than uppercase letters, numbers are earlier than letters, and Russian letters are later than English ones.
 
When comparing characters or strings, Python converts the characters to their respective ordinal values ​​and then compares from left to right. 

For example: "locomotive"  < "steamboat", because the words differ in the fifth letter and "in" < "x".

Splitting a line into parts

When entering a string, you can immediately divide it into parts by any separator.
We've done this before when we entered multiple numeric variables on the same line. We used the split() method to split a string into parts. By default, it separates strings into substrings by space.
Using this method, you can split the string into parts, for example, by spaces. And write each part of the string into a separate variable.
 
Example
s1, s2 = input().split()

In the example, when entering two words separated by a space, the first word is stored in the variable s1, the second - in the variable s2.

Addition and multiplication of strings

Strings can be added, for this the sign "+" is used. This operation is called string concatenation or concatenation
 
Example 
s = "Hello,"
s1 = "world"
print(s + s1)
The screen will display the phrase "Hello world" (without quotes).

Python implements the operation of multiplying a string by a number: it replaces multiple addition.
 
Example 
string
s = "world "+"world "+"world "+"world "
can be replaced by  
s = "world " *4

Referring to row indices

Each character in a string has a number (called index), and numbering always starts from zero in many languages.
In Python, you can specify negative indexes. This means it counts from the end of the line.
 
Example 
String S H e l l o
Index S[0] S[1] S[2] S[3] S[4]
Index S[-5] S[-4] S[-3] S[-2] S[-1]

If you add the length of the string to the negative index, you get a "normal" character position.
 
Need to remember!
In Python, you cannot change a single character in a string, because strings themselves are immutable. 

Iterate over all characters

Since a character can be accessed by index, you can use a variable loop to iterate over all characters, which will take on possible index values. For example, a program that displays all the character codes of the string s would look like this
for i in range(len(s)):
    print(s[i], ord(s[i]))
Explanations for the program:
1) The len(s) function finds the length of a string. The index of the first character is 0 and the index of the last is len(s)-1. The loop variable i will just take values ​​sequentially from 0 to len(s)-1.
2) In each line, the symbol itself will be displayed first, and then its code, which is returned by the built-in function ord().

The same enumeration can be written shorter:
for c in s:
    print(c, ord(c))
In this fragment, the loop header loops through all the characters s, placing them in turn in the variable c.

As already mentioned, the peculiarity of Python when working with strings is that strings are immutable objects. In other words, we cannot change individual characters of a string.

For example, the following statement will not work
s[5]='a'
But you can compose a new line from the characters with the required changes.
 
Task
In the input string, replace all characters 'a' to characters 'b'. 
s = input()
sNew = ""
for c in s:
    if c == 'a': c = 'b'
    sNew += c
print(sNew)

In this program, the loop goes through all the characters of the string s. In the body of the loop, we check the value of the variable с: if the symbol matches the symbol 'a', then we replace it with 'b< /code>' and add it to the end of the new line sNew using the addition operator.
This option is rather slow.

In the future, we'll take a look at the built-in string manipulation functions and learn how to do it faster.