Python – Strings – A quick walk through with Examples

Strings are another sequence types in Python. These are the commonly used types while writing the programs in Python. And these are very useful when we deal with data. Strings in Python are object of str class.

str class provides lot of useful functions to deal with data. But I am not going to cover all of its features in this Article; but will quickly walk through with examples.

Define strings

There are multiple ways, we can define a string in Python. We can use, single quotes (‘), double quotes (“) or using str class to define a string. Strings are immutable; that means, we can’t modify its’ content once the string has been created.

Here I am going to define 3 strings, using different ways.

>>> s1 = 'Apple'
>>> s1
>>> s2 = "Banana"
>>> s2
>>> s3 = str("Custard-Apple")
>>> s3

When you define the strings; you have to keep in mind that, handle the special characters with caution. You can include spaces, newlines, tabs etc, characters inside the string. To use them, you need to use escape characters (starts with backslash – “\”); for example, to print the tab character; use the escape sequence “\t”.

Not only the special characters; to use the characters like backslash – “\”, single quote – (‘), double quote – (“) characters you need to use escape character. for example, to use backslash, you must use the escape character (“\\”).

If Python interpreter finds any escape character in the string literal, it converts into specific character. If you want NOT to convert the escape characters; means, if you want to consider the same characters what you have entered; then you need to define a raw string literals. The string literals prefixed with “r” are raw strings. If you add escape characters to raw strings; those characters also considered as normal characters. for example, below statements clarifies this:

>>> rs = r"No tab\t"
>>> print(rs)
No tab\t
>>> es = "With tab\t!"
>>> print(es)
With tab        !

Observe that, with raw strings; no conversions were done; “\t” printed as it is. But with normal strings; “\t” converted to tab (multiple spaces) and printed in the output; see the gap between “With tab” & “!”.

Access string elements

Like other sequence types, we can access elements from strings using index operator. The index will start from “0” and the index of last element is (length of the string – 1). We can also use negative indexes to access the string elements from the end. For example, below statements access the first and last elements from the string (s1 = ‘Apple’):

>>> s1[0]
>>> s1[-1]

Like other sequence types; we can loop through and display each of its’ element. Below example, shows this:

>>> for x in s1:
...    print(x)

This is the quick walk through about strings in Python. Still lot of things we need to discuss about strings; we will discuss them in my upcoming Articles.

Always, don’t forget to give your feedback through below Comments.


Python – Strings – A quick walk through with Examples

Leave a Reply

Your email address will not be published.

Scroll to top