Strings
On this page
Creating StringsCombining and Repeating StringsAccessing CharactersUseful String OperationsChanging CaseCounting and FindingReplacing and CheckingMini Project: Welcome Message GeneratorStrings are used to represent text in Python—words, sentences, or any sequence of characters. They are one of the most common data types in every program because most software needs to display or process text: names, messages, file names, or even data from users.
In this section, you will learn how to create, combine, and manipulate strings, as well as how to access specific characters and apply useful operations to make your text handling flexible and powerful.
Creating Strings
A string in Python is simply text enclosed within quotation marks. It can be written with either single quotes ('...') or double quotes ("..."). Both are valid and behave the same.
A variable is a name for a value; = assigns the value on its right to the name on its left. The Variables section develops this idea.
name = "Ada Lovelace"
greeting = 'Hello, world!'In both cases, Python treats the value as text.
For longer text that spans multiple lines, triple quotes ("""...""" or '''...''') can be used.
print("""This is
a multi-line
string.""")Output
This is
a multi-line
string.
Strings are everywhere—used in displaying messages, storing user names, saving file paths, or even writing logs and labels.
Combining and Repeating Strings
Strings can be combined or multiplied to create new ones.
To join two or more strings, use the + operator. This is called string concatenation. To repeat a string several times, use the * operator.
print("Hello" + " " + "Python")
print("Hi! " * 3)Output
Hello Python
Hi! Hi! Hi!
This ability is especially useful when constructing messages or formatting text dynamically. For instance, printing a welcome message multiple times or joining words to form sentences.
Accessing Characters
Strings are sequences of characters, and each character has a position known as an index. Indexing allows you to access specific characters within a string. In Python, indexing starts at 0 for the first character.
print("Python"[0]) # first character
print("Python"[1]) # second character
print("Python"[-1]) # last characterOutput
P
y
n
The negative index -1 refers to the last character,-2 to the second last, and so on. This makes it easy to extract or inspect parts of a string.
You can also take slices of a string to get a range of characters.
print("Programming"[0:6]) # characters 0 to 5
print("Programming"[3:]) # from index 3 to end
print("Programming"[:5]) # from start to index 4Output
Progra
gramming
Progr
Slicing is very powerful when processing text, like extracting usernames from email addresses or abbreviations from words.
Useful String Operations
Python provides many operations and functions to make string handling easier.
Changing Case
A method is an operation called through a value using a dot and parentheses, as in "ADA".lower(). Here the operation returns new text.
You can convert text to lowercase, uppercase, or title case depending on the formatting you need. For example, when collecting names from users, you might convert them all to a consistent format so that entries like "ADA LOVELACE", "ada lovelace", or "aDa lOvElAcE" appear neatly as "Ada Lovelace".
print("ADA".lower())
print("ada lovelace".upper())
print("ada lovelace".title())Output
ada
ADA LOVELACE
Ada Lovelace
This is often used for cleaning or standardizing user input.
Counting and Finding
Python can count occurrences of characters or find where a substring appears.
print("banana".count("a"))
print("banana".find("na"))Output
3
2
The count() method tells how many times something appears in the string, while find() gives the index of the first match (or -1 if not found).
Replacing and Checking
Strings in Python are immutable, that is, they cannot be changed directly after they are created. This means you cannot modify individual characters inside a string.
However, you can easily create a new, modified version using the replace() method.
print("I like Java".replace("Java", "Python"))Here, the original string remains unchanged; instead, Python returns a new string where “Java” has been replaced with “Python”. This approach is useful when you need to clean, update, or customize text before displaying or saving it.
Mini Project: Welcome Message Generator
Write a short program that displays a friendly welcome message using string operations. It should take a user’s first and last name (entered as text) and display a properly formatted greeting. Start with first and last names assigned in the code. After Input and Output introduces input(), extend the program to ask for them. Use title() and string concatenation to format the greeting.
Example Run:
Enter your first name: jon
Enter your last name: doe
Hello, Jon Doe! Welcome to Python programming.