Hello Learners…
Welcome to the blog…
Table Of Contents
- Introduction
- Full Stack Data Science Journey: String And List Indexing – 4
- 1.3 String, List, And Indexing
- Summary
- References
Introduction
In this post, we discuss string, and, list indexing in python programming. It is a part of the Full Stack Data Science Journey. It is the 4th post of the series.
Full Stack Data Science Journey: String And List Indexing – 4
As a part of the Full Stack Data Science Journey first, we are learning and implementing python programming in which today we are talking about string, list, and indexing.
1. Python Programming
In Python programming, we are going to follow the below steps
- 1.1 Python Installation and Setup
- 1.2 Python Basic
- 1.3 String, List, And Indexing
- Current Post
So let’s start.
1.3 String, List, And Indexing
First Let’s Start with the String In Python Programming Language.
String
The string data type represents text and characters. let’s define a string variable.
We can define a string in three(3) ways.
Double Quotes
s="galaxyofai"
Single Quotes
s='galaxyofai'
Triple Quotes for multiline string declaration.
s=""" Hi we are from galaxyofai
We are machine learning engineers"""
Here below we can see the output of all the above variables.
String Indexing
string indexing refers to the process of accessing individual characters in a string by specifying their position (index) within the string.
The index starts at 0 for the first character, 1 for the second character, and so on.
Let’s take a string,
s1="galaxyofai"
The indexes are as below,
- g=0
- a=1
- l=2
- a=3
- x=4
- y=5
- o=6
- f=7
- a=8
- i=9
s1[8]
print(s1[8])
When we try to access the 8th character of the string.
#OUTPUT a
String Negative Indexing
You can also use negative indexing to access characters from the end of the string, where -1 refers to the last character, -2 to the second-to-last, and so on:
Negative indexes are,
- g= -10
- a= -9
- l= -8
- a= -7
- x= -6
- y= -5
- o= -4
- f= -3
- a= -2
- i= -1
s1="galaxyofai"
When we try to access characters using negative indexing.
print(s1[-3])
#OUTPUT
'f'
List
A list is a data type used to store a collection of items.
Lists are ordered, mutable (we can change their elements), and allow duplicate elements.
Lists are created using square brackets []
and can contain a mix of different data types, including numbers, strings, and other objects.
Here’s an example of how we can create a list in Python:
my_list = [1, 2, 3, "galaxyofai", "ai","ml"]
we can check the data type of my_list using the type function.
print(type(my_list))
#OUTPUT
list
We also create an empty list without any elements
my_list=[]
print(type(my_list))
#OUTPUT
list
List Indexing
To access elements in a list, we can use indexing, similar to how it works with strings.
List indexing is zero-based, meaning the first element is at index 0, the second element is at index 1, and so on.
my_list = [1, 2, 3, "galaxyofai", "ai","ml"]
# Access individual elements
first_element = my_list[0] # 1
second_element = my_list[1] # 2
third_element = my_list[2] # 3
fourth_element = my_list[3] # "galaxyofai"
fifth_element = my_list[4] # "ai"
# Negative indexing (counting from the end of the list)
last_element = my_list[-1] # "ml"
second_last_element = my_list[-2] # "ai
We can also use slicing to access multiple elements from a list.
Slicing allows us to specify a range of indices to extract a sublist from the list:
The format for slicing is my_list[start_index:end_index]
, where start_index
is inclusive, and end_index
is exclusive.
Summary
strings are used to work with text data and are immutable, while lists are versatile data structures for storing collections of items, and they are mutable.
Both strings and lists use indexing to access their elements, with indexing starting at 0 for the first element.
Happy learning And Keep Learning…
Thank you…