Difference Between List & String in Python

 

Hi! This short tutorial will explain to you the difference between lists and strings in the Python programming language.

The table of content is structured as follows:

Let’s get into the discussion and Python code!

 

Python List

A Python list is used to store different data types, like strings, integers, lists, etc. Below are examples of Python lists containing integers, strings and user-defined objects from top to bottom.

# list of integers
int_list = [5,8,10,4,3,7,1]
print(int_list)
 
# [5, 8, 10, 4, 3, 7, 1]
 
 
 
# list of strings
str_list = ["dog", "hamster", "cat", "fish", "pig"]
print(str_list)
 
# ['dog', 'hamster', 'cat', 'fish', 'pig']
 
 
 
# list of object
class Fruits:
  def __init__(self, fruit, quantity):
    self.fruit = fruit
    self.quantity = quantity
 
my_list = []
my_list.append(Fruits("Oranges",50))
my_list.append(Fruits("Apples",25))
my_list.append(Fruits("Pear",30))
my_list.append(Fruits("Grapes",16))
my_list.append(Fruits("Bananas",42))
 
# for loop to print object list
for l in my_list:
  print(f"{l.fruit}({l.quantity})")
 
# Oranges(50)
# Apples(25)
# Pear(30)
# Grapes(16)
# Bananas(42)

We can also create a Python list using the list() function:

# list of integers
int_list = list((5,8,10,4,3,7,1))
print(int_list)
 
# [5, 8, 10, 4, 3, 7, 1]
 
 
# list of strings
str_list = list(("dog", "hamster", "cat", "fish", "pig"))
print(str_list)
 
# ['dog', 'hamster', 'cat', 'fish', 'pig']

In the examples above, we parsed a tuple to the list constructor, which returned a list of integers and strings.

 

Attributes of Python List

Below are the attributes of Python list:

  • A list is an ordered collection of elements, where each element has a specific index starting from 0.
  • Lists are mutable, which means you can add, remove, or modify elements after the list is created.
  • The elements in a list can be accessed by their index values.
  • The indexes of a list are always integers.
  • A list can hold duplicate values.

 

Python String

A Python string is a data structure that represents a sequence of characters and is typically wrapped in quotes. Below are examples of Python strings:

# character string
chr_string = "happy and smiling"
print(chr_string)
 
# happy and smiling
 
 
print(type(chr_string))
 
# <class 'str'>
 
 
 
# integer string
int_string = "123456789"
print(int_string)
 
# 123456789
 
 
print(type(int_string))
 
# <class 'str'>

We can also use the built-in str() function to create strings in Python:

# character string
chr_string = str("happy and smiling")
print(chr_string)
 
# happy and smiling
 
 
print(type(chr_string))
 
# <class 'str'>
 
 
 
# integer string
int_string = str((123456789))
print(int_string)
 
# 123456789
 
 
print(type(int_string))
 
# <class 'str'>

 

Attributes of Python String

Below are the attributes of Python string:

  • Python strings are immutable, which means they cannot be changed or altered after they have been created.
  • Strings can be sliced to create a new sequence of characters. Slicing a string returns a new string that contains a specified subset of the original string.
  • Strings can be concatenated using the + operator. This creates a new string that contains the characters of both strings.
  • The split() method can be used to split a string into a list of substrings, based on a specified delimiter.
  • Characters in a string can be accessed using their index, which starts at 0.
  • The len() function can be used to determine the length of a string, which is the number of characters it contains.

So, although a Python list and Python string may be two different data structures, they can be used together in a way that allows for more robust data processing and manipulation capabilities.

With that, we have examined the differences between lists and strings in Python. I hope you found this tutorial helpful!

Video, Further Resources & Summary

Do you need more explanations on the differences between lists and strings in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail the differences between lists and strings in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown the differences between lists and strings in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top