Fundamentals 9 min read

Master Python Data Structures: Strings, Lists, Tuples, Dictionaries & Sets

This guide walks through Python’s fundamental data structures—strings, lists, tuples, dictionaries, and sets—showing how to create, manipulate, and query them with clear code examples for operations such as slicing, joining, sorting, and set mathematics.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Data Structures: Strings, Lists, Tuples, Dictionaries & Sets

1. Strings

Demonstrates common string operations in Python such as capitalize, count, center, endswith, isdigit, join, split, strip, and formatted output.

name = 'derek'
print(name.capitalize())          # Derek
print(name.count("e"))            # 2
print(name.center(10, '*'))       # ***derek***
print(name.endswith('k'))          # True
print('244'.isdigit())             # True
print('+'.join(['1', '2', '3']))   # 1+2+3
print('
123'.strip())            # 123
print("1+2+3+4".split('+'))      # ['1', '2', '3', '4']
msg = 'my name is {name} and i am {age} old'
print(msg.format(name='derek', age=20))  # my name is derek and i am 20 old

2. Lists

Shows list creation, slicing, appending, removing, popping, inserting, modifying, extending, counting, sorting, reversing, and enumerating elements.

fruit = ['apple', 'pear', 'grape', 'orange']
print(fruit[1])            # pear
print(fruit[1:3])          # ['pear', 'grape']
print(fruit[-1])           # orange
fruit.append('peach')
print(fruit)
fruit.remove('peach')
print(fruit)
fruit.pop()
print(fruit)
del fruit[2]
print(fruit)
fruit.insert(1, 'grape')
print(fruit)
fruit[2] = 'orange'
print(fruit)
fruit1 = ['apple', 'orange']
fruit2 = ['pear', 'grape']
fruit1.extend(fruit2)
print(fruit1)
print(fruit1.count('apple'))
fruit1.sort()
print(fruit1)
fruit1.reverse()
print(fruit1)
for index, item in enumerate(fruit1):
    print(index, item)

3. Tuples

Illustrates tuple creation and basic methods count and index.

fruit = ('apple', 'orange', 'grape')
print(fruit.count('apple'))   # 1
print(fruit.index('orange'))  # 1

4. Dictionaries

Shows dictionary creation, adding, modifying, deleting entries, retrieving values, and iterating over keys, values, and items.

fruit = {1: 'apple', 2: 'orange', 3: 'grape'}
print(fruit)
fruit[4] = 'pear'
print(fruit)
fruit[4] = 'peach'
print(fruit)
fruit.pop(4)
print(fruit)
print(fruit.get(1))
for k, v in fruit.items():
    print(k, v)
for k in fruit.keys():
    print(k)
for v in fruit.values():
    print(v)

5. Sets

Demonstrates set creation, adding single or multiple elements, removing, popping, and set operations such as union, difference, and intersection.

fruit = set(['apple', 'orange', 'pear'])
fruit.add('grape')
fruit.update(['peach', 'banana'])
fruit.remove('banana')
fruit.pop()
num1 = set([11, 22, 33, 44])
num2 = set([33, 44, 55, 66])
print(num1.union(num2))        # {66, 11, 22, 33, 44, 55}
print(num1.difference(num2))   # {11, 22}
print(num1.intersection(num2)) # {33, 44}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data StructuresSetstuplesListsStringsDictionaries
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.