What is the str__ object in Python

The str object and its methods

The character string should now be examined more closely. What can you do with it? The order of the individual paragraphs will probably change more often, because the text is a patchwork of many small entries that have been accumulated for a long time.
So please be sorry.

>>> print ('michael' + 'poeltl') michael poeltl >>>

is an important principle. Combining parts to a new whole is also available for lists and tuples.

>>> (1,2,) + ('a', 'b') (1, 2, 'a', 'b') >>> [1,2] + ['a' + 'b'] [1, 2, 'from'] >>>

Duplicating a part with the operator is also a great thing. What is remarkable here, again, is that the result is not a whole lot of parts, but again only one object.

>>> print ('Ni' * 10) Ni Ni Ni Ni Ni Ni Ni Ni Ni >>> print ('{} \ n {}'. format (myname, '=' * len (myname))) michael poeltl ===============

This can also be done with other object types, such as, for example, lists.

>>> ['list'] * 5 ['list', 'list', 'list', 'list', 'list'] >>> ['one', 'list'] * 5 ['one', 'list', 'one', 'list', 'one', 'list', 'one', 'list', 'one', 'list'] >>> [['list']] * 5 [[ 'list'], ['list'], ['list'], ['list'], ['list']] >>> >>>

A sequential piece is lengthened by a factor of x. Would that also work with other types, such as a mapping type?

>>> {'one': 1} * 5 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type (s) for *: 'dict' and ' int '>>>

So we see; that flew right around our ears.

escape sequences

It could well be that I have already shown the escape sequences elsewhere.
Escape because the information is introduced with a backslash.
For example, to set a tab, you typed

so \ t.

\ 'single quote \ "double quote \ guess three times \ a bell \ b backspace \ f formfeed \ n newline \ r carriage return \ t tab \ v vertical tab

There is still octal, hexadecimal and unicode escape sequences.
Characters in the ASCII table, e.g., can be identified using octal or hexadecimal being represented.

>>> print (ord ('m')) 109 >>> print (hex (ord ('m'))) 0x6d >>> print (oct (ord ('m'))) 0o155 >>> hex ( 0o155) '0x6d' >>> oct (0x6d) '0o155' >>> int (0x6d) 109 >>> chr (0x6d) 'm' >>>

is so
==> decimal 109 ==> octal 155 and ==> hexadecimal 6d

>>> print ('m', '\ 155', '\ x6d') m m m >>>

So here we see an octal and a hexadecimal escape sequence ('\ 155', '\ x6d').
It gets more interesting with characters such as newline \ n
\ n in octal and in hexa.

>>> print ('\ n', '\ 012', '\ x0a') >>>

You can see a lot out of all the whitespace ;-)
In python3.X there are strings by default unicode-strings -> many (if not all) characters of the various languages ​​can be displayed.

The unicode table is of course much larger than the ascii table (which is a small subset of the unicode table).
Here is just a small sample - later (not in the basics part) we can / will deal with unicode in more detail.

>>> #letter a >>> ord ('a') 97 >>> ord ('\ N {latin small letter a}') 97 >>> unicode_a = '\ N {latin small letter a}' >> > print (unicode_a) a >>> print (ord (unicode_a)) 97 >>> print (oct (97), hex (97)) 0o141 0x61 >>> #french a with acute >>> print (ord (' \ N {latin small letter a with acute} ')) 225 >>> print (oct (225), hex (225)) 0o341 0xe1 >>> print (' \ 341 ',' \ xe1 ',' \ u00e1 ' ) á á á >>>

At the end was the '\ u00 ' spelling;
With chr () you can infer the sign from the number.
chr () is handy for the next tasting (and then we'll leave it at that for the time being - we'll meet here and there).

>>> for i in range (240,265): ... print (i, '\ t', chr (i)) ... 240 ð 241 ñ 242 ò 243 ó 244 ô 245 õ 246 ö 247 ÷ 248 ø 249 ù 250 ú 251 û 252 ü 253 ý 254 þ 255 ÿ 256 Ā 257 ā 258 Ă 259 ă 260 Ą 261 ą 262 Ć 263 ć 264 Ĉ >>>

to int () one further amendment!
int () returns a decimal number (number base 10).

>>> number10 = '128' >>> number10 = int (number10) >>> print (number10, type (number10)) 128 >>>

What if we wanted to convert a string that actually expresses an octal number to an int?

>>> szahl8 = oct (1024) >>> print (szahl8, type (szahl8)) 0o2000 >>> print (int (szahl8)) Traceback (most recent call last): File " ", line 1, in ValueError: invalid literal for int () with base 10: '0o2000' >>>

The addition to int () is now the second parameter of this built-in function, which specifies what the string is to be seen as.

>>> print (int (s number8, 8)) 1024 >>>

and another example:

>>> print (int ('1000', 2), int ('1000', 8), int ('1000', 10), int ('1000', 16)) 8 512 1000 4096 >>> print ( int ('ffffff', 16)) 16777215 >>>

simple string operations

before the first method a known built-in function - sorted ()

>>> first name = 'nico' >>> sorted (first name) ['c', 'i', 'n', 'o'] >>>

sortedIn this case () received a character string and outputs it sorted as listObject back. Sorting is from to.
You can use to reverse the sorting order with sorted ().

>>> sorted (first name) [:: - 1] ['o', 'n', 'i', 'c'] >>>

So we reversed the order using the familiar trick for the list object. BUT, we want to use a parameter from the sorted function.

>>> sorted (first name, reverse = True) ['o', 'n', 'i', 'c'] >>> = exec () - built-in-function python code in a string, is created using exec () executed >>> s = "for i in range (10): \ n \ tprint (i) \ n" >>> exec (s) 0 1 2 3 4 5 6 7 8 9 >>> every program line must end with \ n; there must also be \ n at the end. Indentation is indicated by \ t. = common methods on str objects: >>> first name = 'nico' >>> (here comes first name.

Here it goes to the top of the page and