If you already work with data — maybe in Tableau, Alteryx, or SQL — but you’re dipping your toes into Python, this guide’s for you. This is your quick-grab cheat sheet for string functions in Python — all the essentials you’ll need when cleaning, formatting, or exploring text data.
Here’s what we’ll go through:
- type()
- str()
- len()
- count()
- replace()
- concatenation / joining strings
- f-strings
- split()
- string repetition
- indexing and slicing
- strip()
- changing cases: upper() / lower()
- searching: in, startswith(), endswith()
- validation: isalpha(), isnumeric()
Let’s jump in
🔹 Type()
Want to know what type of data you’re dealing with? Use type()
to return the data type.
Example:
name = 'Rosh'
print(type(name))
Output: <class 'str'>
🔹 Str()
Need to turn numbers or other data types into text? Use str()
.
Example:
age = 25
print(str(age))
Output: 25
🔹 Len()
Counts how many characters (including spaces) are in a string.
Example:
name = 'Rosh'
print(len(name))
Output: 4
🔹 Count()
Counts how often a word or letter appears in your string.
Example:
text = 'Python Python Python'
print(text.count('Python'))
Output: 3
🔹 Replace()
Swaps one part of a string with another.
Example:
price = "1234,56"
print(price.replace(',', '.'))
Output: '1234.56'
🔹 Concatenation (Add Strings)
You can combine (or “concatenate”) strings using the +
operator.
Example:
first_name = 'Rosh'
last_name = 'Khan'
print(first_name + ' ' + last_name)
Output: 'Rosh Khan'
🔹 F-Strings (Formatted Strings)
F-strings are the cleanest way to include variables directly inside strings.
Example:
name = "Rosh"
age = 25
print(f"My name is {name} and I'm {age} years old.")
Output: My name is Rosh and I'm 25 years old.
Don’t forget the f
at the start of the string, or it won't work.
🔹 Split()
Breaks a string into parts based on a separator.
Example:
value = "Rosh-25-UK"
print(value.split('-'))
Output: ['Rosh', '25', 'UK']
🔹 String Repetition
Repeats a string multiple times using *
.
Example:
print("ha" * 3)
Output: 'hahaha'
🔹 Indexing and Slicing
Grab parts of a string by position. Python starts counting at 0, not 1, so the first character is position 0
.
You can also use negative indexing to count backwards from the end of a string, but note that it starts at -1, not 0. So -1
is the last character, -2
is the one before that, and so on.
Example:
word = "example"
print(word[0:4]) # First 4 letters
print(word[-1]) # Last character (starts from -1)
print(word[-3:]) # Last 3 characters
Output:
exam
e
ple
🔹 Clean Whitespace (strip, lstrip, rstrip)
Use strip()
to remove spaces from the start and end of your string.
You can also be more specific:
lstrip()
removes characters from the left side only.rstrip()
removes characters from the right side only.
And it’s not just about spaces. You can tell these functions which characters to strip.
Example:
text = " Example "
print(text.strip()) # Removes spaces on both sides
print(text.lstrip()) # Removes spaces on the left only
print(text.rstrip()) # Removes spaces on the right only
Output:
'Example'
'Example '
' Example'
🔹 Case Conversion
Switch between upper and lower case.
Example:
text = "Python"
print(text.upper())
print(text.lower())
Output:
PYTHON
python
🔹 Searching Strings
You can check if something exists inside a string using in
, or use startswith()
and endswith()
.
Example:
phone = "07654321098"
print("765" in phone)
print(phone.startswith("07"))
print(phone.endswith("98"))
Output:
True
True
True
🔹 Validation Methods
Use these to check what kind of characters your string contains (letters or numbers)
Example:
name = "Rosh"
age = "25"
print(name.isalpha())
print(age.isnumeric())
Output:
True
True
If you’re learning Python, keep this page handy. Strings show up everywhere — from file names to user input — and mastering them early will make your Python life way easier.