The name str in Python is used to denote strings. This is a built-in data type that is an ordered sequence of Unicode characters. Typically, strings contain textual information. They are similar to C language arrays, but have a number of powerful processing tools.
Line literals
There are several ways to write strings. The most popular are quotation marks and apostrophes. They are interchangeable and their use allows to exclude the backslash character "\":
- >>> Example_1 = "This is how the lines are written"
When processing strings in Python, triple quotes are also allowed. It is convenient to enclose large blocks of text in them. Inside the design, apostrophes and ordinary quotation marks may be present:
- >>> Example_2 = "" "An approximate set of words for the" block line "in Python" "ยป
Basic operations
Strings support the pressure of standard operations for sequences. These are concatenation, indexing, slice extraction, length calculation and repetition:
- >>> Page_1 = "FB" # Assign a value
- >>> Page_1
- "FB"
- >>> Page_2 = Page_1 + "." + "Ru" # perform concatenation
- >>> Page_2
- FB.ru
- >>> Page_3 = "O" * 3 + "PS!" # repetition and concatenation
- >>> Page_3
- "OOOPS!"
- >>> len (Page_3) # length calculation
- 6
Since in Python str are immutable types, each operation creates a new string object.
Row indexing
Each element of the string can be accessed by its position or by serial number. The countdown starts not from the usual unit, but from scratch. To work with indexes, square brackets are used. Therefore, if you want to extract the second character, you need to pass the command โobject nameโ [1] to the interpreter:
When extracting a slice, the number to the left of the operator โ:โ means the left border inclusively. The number on the right indicates the item to which the slice will be extracted. It is important to remember that the object indicated to the right of the colon does not enter the slice:
String conversion
In Python, str () can be called as a built-in function. As an argument, it takes any objects and returns their string representation. For example, if you need to perform concatenation, there should be data of the same type on both sides of the โ+โ sign. Otherwise, the interpreter will display an error message:
- >>> 5+ "dogs" + "run"
- Traceback (most recent call last): ... TypeError
- >>> str (5) + "dogs" + "run."
- "5 dogs run."
Instead of str (), another repr () function is allowed. It also performs the conversion, but returns the object as a line of code in the advanced version.
For advanced string processing, there is a powerful set of methods specific to this type of data. Formally, these are attributes attached to objects that reference functions.
The syntax for using string methods is as follows: โobject.method (argument)โ.
- >>> l = "ggffkkllrr"
- >>> l.replace ("ff", "gg")
- "Ggggkkllrr"
The example used the method of replacing .replace () elements. This universal method accepts strings of any length as arguments and performs a global search with subsequent replacement.
There are other methods and operators for working with strings, their formatting and conversion. A complete listing is in the official language guide.