Fundamentals 5 min read

Python Data Types: Tuple and String

This article introduces Python's immutable tuple and string data types, explains their characteristics, demonstrates how to convert between tuples, lists, and strings, and provides practical code examples for common operations such as indexing, slicing, counting, and formatted printing.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Python Data Types: Tuple and String

Tuple (tuple) – Tuples are similar to lists but immutable; once created they cannot be modified. Operators ( in , + , * ) and built‑in functions ( len , max , min ) work the same as with lists because they do not alter elements. Indexing and slicing retrieve elements, but methods for adding, removing, or replacing are unavailable; only count and index methods exist. Tuples are often used as dictionary keys for efficiency when mutability is not required.

A single‑element tuple must include a trailing comma. Elements can be of any type, including strings, other tuples, lists, dictionaries, functions, file objects, and numbers.

Conversion between list and tuple can be done with the built‑in functions tuple() and list() :

>> x = [1,2,3,4]
>> tuple(x)
(1, 2, 3, 4)
>> print(type(x))
<class 'list'>

Conversely, a tuple can be turned into a list:

>> x = (1,2,3,4)
>> list(x)
[1, 2, 3, 4]
>> print(type(x))
<class 'tuple'>

String (string) – Python excels at string processing. Strings can be defined with single, double, triple‑single, or triple‑double quotes and may contain tab (\t) and newline (\n) characters. Strings are immutable; operations and function calls return new strings.

Operators ( in , + , * ) and built‑in functions ( len , max , min ) behave the same as with lists and tuples. Indexing and slicing retrieve substrings, but cannot modify the original string. The re module provides additional regex‑based processing.

>> x = "live and   let \t  \tlive"
>> x.split()
['live', 'and', 'let', 'live']
>> x.replace(" let \t  \tlive", "enjoy life")
'live and   enjoy life'
>> import re
>> regexpr = re.compile(r"[\t]+")
>> regexpr.sub(" ", x)
'live and   let    live'

The print function outputs strings and automatically converts other objects to their string representation. The % operator offers classic printf‑style formatting.

>> e = 2.85
>> x = [1,"two",3,4.0,["a","b"], (5,6)]
>> print("the constant e is:", e, "and the list x is:", x)
the constant e is: 2.85 and the list x is: [1, 'two', 3, 4.0, ['a', 'b'], (5, 6)]
>> print("the value of %s is: %.2f" % ("e",e))
the value of e is: 2.85

When using print , objects are automatically converted to their string form, and the % operator provides formatting capabilities.

pythonData Typesstringfundamentalstuple
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login 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.