A string in Python is a series of characters enclosed into quotes or double quotes:
You can assign a string value to a variable:
name = "Roger"
You can concatenate two strings using the
phrase = "Roger" + " is a good dog"
You can append to a string using
name = "Roger" name += " is a good dog" print(name) #Roger is a good dog
You can convert a number to a string using the
str class constructor:
This is essential to concatenate a number to a string:
print("Roger is " + str(8) + " years old") #Roger is 8 years old
A string can be multi-line when defined with a special syntax, enclosing the string in a set of 3 quotes:
print("""Roger is 8 years old """) #double quotes, or single quotes print(''' Roger is 8 years old ''')
A string has a set of built-in methods, like:
isalpha()to check if a string contains only characters and it’s not empty
isalnum()to check if a string contains characters or digits and it’s not empty
isdecimal()to check if a string contains digits and it’s not empty
lower()to get a lowercase version of a string
islower()to check if a string is lowercase
upper()to get an uppercase version of a string
isupper()to check if a string is uppercase
title()to get a capitalized version of a string
startsswith()to check if the string starts with a specific substring
endswith()to check if the string ends with a specific substring
replace()to replace a part of a string
split()to split a string on a specific character separator
strip()to trim the whitespace from a string
join()to append new letters to a string
find()to find the position of a substring
and many more.
None of those methods alter the original string. They return a new, modified string instead. For example:
name = "Roger" print(name.lower()) #"roger" print(name) #"Roger"
You can use some global functions to work with strings, too.
In particular I think of
len(), which gives you the length of a string:
name = "Roger" print(len(name)) #5
in operator lets you check if a string contains a substring:
name = "Roger" print("ger" in name) #True
Escaping is a way to add special characters into a string.
For example, how do you add a double quote into a string that’s wrapped into double quotes?
name = "Roger"
"Ro"Ger" will not work, as Python will think the string ends at
The way to go is to escape the double quote inside the string, with the
\ backslash character:
name = "Ro\"ger"
This applies to single quotes too
\', and for special formatting characters like
\t for tab,
\n for new line and
\\ for the backslash.
Given a string, you can get its characters using square brackets to get a specific item, given its index, starting from 0:
name = "Roger" name #'R' name #'o' name #'g'
Using a negative number will start counting from the end:
name = "Roger" name[-1] #"r"
You can also use a range, using what we call slicing:
name = "Roger" name[0:2] #"Ro" name[:2] #"Ro" name[2:] #"ger"
More python tutorials:
- Introduction to Python
- Installing Python 3 on macOS
- Running Python programs
- Python 2 vs Python 3
- The basics of working with Python
- Python Data Types
- Python Operators
- Python Strings
- Python Booleans
- Python Numbers
- Python, Accepting Input
- Python Control Statements
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python Functions
- Python Objects
- Python Loops
- Python Modules
- Python Classes
- The Python Standard Library
- Debugging Python
- Python variables scope
- Python, accept arguments from command line
- Python Recursion
- Python Nested Functions
- Python Lambda Functions
- Python Closures
- Python Virtual Environments
- Use a GoPro as a remote webcam using Python
- Python, how to create a list from a string
- Python Decorators
- Python Docstrings
- Python Introspection
- Python Annotations
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get the details of a file
- Python, how to check if a file or directory exists
- Python Exceptions