Strings in Python#
đ Data Analysis Using Python đ Strings & Data Structures Lesson 016
â Previous ¡ Next ✠¡ â Section ¡ â Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Working with text#
Data is full of text â names, categories, codes, addresses â and Pythonâs string type is how text is represented and manipulated. Opening the data-structures stage, this lesson covers strings in Python: creating them, their operations, and the crucial property of immutability. It extends the string work from the spreadsheet and SQL sections into Python.
Creating and combining strings#
A string is text, written in single or double quotes:
name = "North Region"
code = 'NR-001'
Strings combine and repeat with operators:
greeting = "Hello, " + name # concatenation: "Hello, North Region"
line = "-" * 20 # repetition: 20 dashes
The + concatenates strings (as in the spreadsheetâs & and SQLâs CONCAT), and
* repeats a string â the basic ways to build text.
String methods#
Strings are objects (the OOP lesson) with many useful methods, mirroring the string functions from earlier sections:
text = " North Region "
text.strip() # "North Region" â remove surrounding whitespace (like TRIM)
text.upper() # " NORTH REGION " â uppercase
text.lower() # lowercase
text.replace("North", "South") # substitute (like SUBSTITUTE / REPLACE)
"NR-001".split("-") # ["NR", "001"] â split on a delimiter
len("North") # 5 â length (like LEN)
These are the same cleaning and manipulation operations from the spreadsheet (TRIM,
UPPER, SUBSTITUTE) and SQL (TRIM, UPPER, REPLACE, SUBSTR) â now as
Python string methods, called with dot notation on the string object.
String immutability#
A crucial property: strings in Python are immutable â once created, a string cannot be changed in place. String methods do not modify the original; they return a new string:
text = "north"
text.upper() # returns "NORTH", but...
print(text) # still "north" â unchanged!
text = text.upper() # to keep the result, reassign
print(text) # now "NORTH"
This catches many beginners: calling text.upper() does not change text; you must
assign the result back. Immutability means string operations produce new strings, and
using the result requires capturing it â a fundamental and frequently-forgotten point.
Why strings matter#
Text manipulation is constant in data work â cleaning categories, parsing codes, extracting parts, formatting output â and Pythonâs string methods are the tools for all of it, more flexible than their spreadsheet and SQL counterparts. Because so much real data is text (or arrives as text needing conversion, the type lesson), fluency with strings is essential to Python data analysis. The following lessons go deeper into indexing, slicing, and formatting strings.
The caveat#
String immutability is the pitfall to internalise: the single commonest string mistake is
calling a method and expecting the original to change â text.strip() on its own does
nothing lasting; you must write text = text.strip(). Every string âmodificationâ is
really âcreate a new string and (usually) reassign.â Beyond that, strings carry the
encoding and special-character subtleties of all text (the Unicode considerations), and
splitting or extracting assumes a structure that real text may not consistently have (the
defensive-extraction point from the spreadsheet strings lesson applies). Capture method
results, and handle textâs irregularity. The next lesson covers reaching into strings by
position: indexing and slicing.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/strings-in-python/ (insightful-data-lab.com).