Python Strings with Examples

In this article will talk about Python Strings. If you didn’t see our last article Python break, continue and pass Statements with Examples then click the link and know more about python.

Python strings are a collection of characters contained in quotation marks. The Python programming language has a simple yet powerful concept.

Examples of Python Strings

A Python string can be printed in a variety of ways.

print("Follow us on")
print('Follow us on')

Both will provide the same result: Follow us on

However, you will not be able to print like this:

print("Follow us on')  #this is wrong

This will result in an error if you run this code. Run and try that.

You may also use a variable to store a string and then print it. Something like this :

x = "Follow us on"

It will provide the result like this: Follow us on

A multiline string can also be executed. You’ll need to utilize a triple quotation to do this. Something like this:

x = '''Follow

It will provide the result like this:


Examples of Python Strings with indexing

Let’s look at indexing in a Python string now. Let’s look at some instances to help you understand. We used a string before and assigned it to the variable x. In our case, we’ll make use of it.

>>> x = "Follow us on"
>>> x[0]
>>> x[1]
>>> x[6]
' '
>>> x[18]
>>> x[-1]
>>> x[-25]
  • So, what do we see here? We can see that Python indexing starts with 0. The initial character from the left-hand side of the string is represented by index 0. As a result, when we run the code x[0] and x[1], we get the values ‘F’ and ‘o’.
  • Python also produces a null string while indexing. We can observe that running the code x[6] results in a null string output. You may manually count it from the left-hand side as well. But don’t forget to start at 0 because Python indexing starts at 0.
  • In the last cases when we run the code x[-1], it prints out the final character of the Python string (right-hand side), and when we execute the code x[-25], it prints out the result ‘F’.

Negative indexing is when you start indexing from the right-hand side and go like -1, -2, -3, -4, -…., and so on. And it all begins with a -1. Non-negative indexing is when you start indexing from the left-hand side and go like 0, 1, 2, 3,…, and so on. It begins with a zero.

An Index Error will appear if you input an unknown integer for indexing. As an example:

>>> x[65]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

Example to find Length of String

The len function in Python may be used to determine the length of a string. Something along these lines:

>>> x = "Follow us on"
>>> len(x)

It outputs 25. You may count it manually as well; this variable x will hold 25 characters of the Python string.

Strings in Python are immutable, which means that any attempt to change a component of a string will result in an error:

>>> x[2] = 'Z'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Slicing of Strings

We sometimes need to extract a substring from a string. Slicing is the process of extracting a substring from a string. Consider the following example:

>>> x = "Follow us on"
>>> x[0:6]
>>> x[-18:-4]
'us on Softhunt'

You must give the start and end values while slicing. If you don’t specify a start or end value, Python will use 0 as the default start value and the length of the string as the default end value. For example:

>>> x[:6]
>>> x[15:]

One thing to keep in mind while slicing is to define the start and end values. When we print out the value, Python ignores the final value. for example:

>>> x[0:6]

0 - F
1 - 0
2 - l
3 - l
4 - o
5 - w

Experiment with various slicing values. It will help you visualize things more clearly in your mind.

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

If you want to learn more about the python programming language then click the link to see our last article Python break, continue and pass Statements with Examples

Leave a Comment