Sequences

Sequences

In Python, a sequence is a type of data structure that represents an ordered collection of elements. Sequences are iterable, meaning that we can loop over their elements using a for loop, and they support various operations such as indexing, slicing, and concatenation.

There are three built-in sequence types in Python: lists, tuples, and strings.

Lists and tuples were discussed in previous answers, so in this answer, let’s focus on strings.

A string is a sequence of characters that is represented using either single quotes ' ' or double quotes " ". For example:

my_string = "Hello, World!"

We can access individual characters of a string using their index, just like with lists and tuples:

pythonCopy codefirst_character = my_string[0]

We can also slice a string to get a substring:

substring = my_string[0:5]  # Get the first five characters

Strings are immutable, meaning that we cannot modify their contents. However, we can create new strings by concatenating existing ones using the + operator:

pythonCopy codenew_string = my_string + " How are you?"

We can also use various built-in string methods to manipulate strings, such as split(), replace(), and more.

Overall, sequences are an important and useful data structure in Python, and they are commonly used in many programming tasks.

Apply for Python Certification!

https://www.vskills.in/certification/certified-python-developer

Back to Tutorials

Share this post
[social_warfare]
Dictionary
Set

Get industry recognized certification – Contact us

keyboard_arrow_up