Введение в Python
Теория: Python strings
Полный доступ к материалам
Strings in Python
Common info about strings
- string is the most suitable data type for human
- strings are immutable
- strings are iterable
- strings are case-sensitive
- there are two types of strings in Py2.x
- strings are the main difference b/w Py2 and Py3
How do declare a string
- Use both single or double quotes
- Use triple single or double quotes for multiline strings
- You may combine both types of quotes
Docstrings
- For modules, functions, classes
- The first line is a short description
- The blank line separates long description
- The description
- Stores in obj.doc attribute
- help(obj) to show a docstring
Symbols quoting
- Use
\t,\n,\r,\x,\ufor special symbols - Use
\to quote the following symbol
Common string methods
.lower(),.upper(),.capitalize()to change casestr.split(sep)to split str to a listsep.join(iter)to merge an iterable into a str.replace()to replace text.strip()to remove empty symbols at start and end
Raw strings
r'...'stands for quoted (raw) string- shows symbols as-is
- useful for regexps
How to turn an object to a string
- use built in
strclass - you can override default
strbehaviour
Operation on strings
- Use
+operator to concatenate strings - Use
*operator to repeat a string
String is iterable
Index access and slices
- Syntax is close to lists
- Use
[N]clause to get Nth symbol - You can only read it
- Use
[start:end;Nth]syntax to slice a string
In operator
Next lesson
- unicode strings
- encoding/decoding strings