Strings in Python

Strings are one of Python’s Built-in Data Types, these come under Sequence Bulit-in Python Data Type. Sequence Data Types are collections of objects but in order. So Python Strings are collections of characters having a specific order.
For example – ‘ComputerScienceHub’ is a string in Python.

How to create string in Python?

String in Python can be created by enclosing characters inside single or double quotes. For example – ‘Python’ or “Python” both are strings.

How to find length of string in Python?

For finding length of a string, you can use len() function which is built into language itself. In python, length of a string equals to number of characters the string have.
Lenght of string “Hello” is 5 as it have only five characters.
Length of string “Computer” is 8 as it have eight characters.

x = "Hello"
print(len(x)) // Returns 5

How to access characters inside a string in Python?

Individual characters inside a Python string can be accessed by specifying string name followed by a number inside square brackets[]. For example – To access character at index position 3 from string x = “Hello”, you need to write x[3].
Positive number inside square bracket means start from left, while negative means start from far right end of string.
So for x = “Programming”, x[1] = r while x[-1] = g

Below are some code examples showing How to access characters inside a Python string?
x = "Computer"
x[0]           # Returns C
x[1]           # Returns o
x[2]           # Returns m

y = "Computer Science Hub"
y[0]           # Returns C
y[4]           # Returns u
y[8]           # Returns ' ' as there is nothing in string at index 8

Note
Spaces inside a string are also a part of string itself and have a index position. That’s why in above example – y[8] is returning ‘ ‘

Immutability of Strings in Python?

Strings in Python are immutable meaning once created these can never be overwritten. Like if you add two strings x = “I love”, y = ” Programming” then Python will make a new string “I love Programming” but will not assign it back to either of x, y.
If your a geek like me, you may wonder Why is that Strings are immutable? Strings being immutable improves management management as a fixed/unchanging block of storage is allocated for holding these.

Conclusion

Strings are very useful as these can be used for holding text. Not only that these are also unique owing to properties like Immutability, characters inside being accessible by square brackets.

Also Python have many methods specifically for dealing with Strings. If in case your interested then you can check those out here – Methods for Strings in Python.

Moreover here on this website, I’ve put up a number of articles about Python Programming Language. If your interested then you can check those out here – Python Programming Language Computer Science Hub.

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts