Logo

Strings

Python, in addition to numbers, can modify strings, that can be expressed in a variety of ways. They achieve the same output when enclosed in quote marks ('...') or double quotes ("..."). \ symbol can also be used to separate quotes:

 

 

The output string is enclosed in quotes in the interactive interpreter, and special characters are escaped with backslashes.

While this may look different from the input at times (the enclosing quotes may change), the two strings are equivalent. If the string contains a single quote but no double quotes, it is enclosed in double quotes; otherwise, it is enclosed in single quotes.

By removing the enclosing quotes and printing escaped and special characters, the print() function produces a more readable output:

 

 

If you don't want characters preceded by to be interpreted as special characters, add a r before the first quote:

 

 

String literals are allowed to span multiple lines. One method is to use triple quotation marks: """...""" or "'..."'.

 Ends of lines are instantly included with string, but this can be avoided by adding an at the end of the route. Consider the following example:

 

 

Produces the following output (note that the initial newline is not included):

Strings can be concatenated (joined together) using the + operator and then repeated with *:

 

 

When two or more string literals (those enclosed between quotes) are placed next to each other, they are automatically concatenated.

 

 

This feature comes in handy when you need to break long strings. However, this only works with two literals, not variables or expressions. Use ' +' when you are required to concatenate variables or a variable and a literal.

 

 

Strings could be indexed (subscripted), with index 0 being the first character. There is no such thing as a character type; a character is simply a string of length one:

 

 

Indices can also be negative numbers, counting from right to left:

 

 

Because -0 is the same as 0, negative indices begin at -1. Slicing is supported in addition to indexing. While indexing is being used to retrieve individual characters, sectioning is used to retrieve substrings:

 

 

Take note of how the beginning is always included and the end is always excluded. This ensures that s[:i] + s[i:] always equals s:

 

 

An omitted first index defaults to zero, while an omitted second index defaults to the length of the string being sliced.

 

 

One way to remember how slices work is to imagine the indices as pointing between characters, with the first character's left edge numbered 0. Then, for example, the right edge of the last character of a string of n characters has index n:

Text

Figure 6.1. String indexing in python.

 

The first row of numbers represents the position of the indices 0...6 in the string, while the second row represents the corresponding negative indices. All characters between the edges labeled I and j make up the slice from I to j.

If both indices are all within bounds, the length of a slice for non-negative indices is the difference of the indices. For example, word[1:3] has a length of 2.

An attempt to use an index which is too large will result in the following error:

 

 

However, out of range slice indexes are handled gracefully when used for slicing:

 

 

Python strings are immutable and cannot be changed. As a result, assigning to an indexed position in the string yields an error:

 

 

If you require a different string, you should make one.

 

 

The length of a string is returned by the built-in function len().

 

 

 


________________________________________________________________________________________________________________________________
Footer
________________________________________________________________________________________________________________________________

Copyright © 2022-2023. Anoop Johny. All Rights Reserved.