Please enable JavaScript to use CodeHS

Python Strings

Learn how to create, modify, and implement strings using the Python programming language.

By Ryan Molyneaux

In the world of computer science, a string is known as a finite sequence of characters. They allow us to process character data, which is essential to programming. Python also provides a plethora of operators, methods, and functions that allow the programmer to manipulate strings efficiently and fairly easily.

An embedded gif that reads: str= "I'm a string!"

String Syntax

To create a string, you can bookend your characters with either single quotes or double-quotes. You do have the option of using 3 sets of either type of quote. While it is not required, the latter is generally reserved for strings that take up multiple lines.

The examples below illustrate the multiple ways in which strings can be created in Python.


If you take a close look at example 4, you’ll notice that this string’s formatting is very similar to Python’s multi-line comment structure. While three sets of quotes typically yield a comment for multiple lines, the text still prints onto the screen. This is due to the quotes being placed inline, which means the quotations start and end within a viable block of code that is intended to be read by the program.

In the example below, there are two blocks of code with nearly identical structures. Both possess a variable, string literal, and a print function. However, you’ll notice only one of the blocks of code will print onto the screen. Run the program and feel free to play around with what’s being printed.


String Operations

Concatenation

To join two or more strings together, we can concatenate them using a couple of approaches.

To join strings with strings, you can print them side by side as such:

snack = "PB " "& " "J "
print (snack)

PB & J
Plain text

Keep in mind that the above method only works with string literals. If any of your strings are stored in variables, we would need to use the + operator to join them together.

schmear_1 = "PB "
schmear_2 = "J "

print (str(schmear_1) +"& " + str(schmear_2))

PB & J
Plain text

Note: Variables schmear_1 and schmear_2 have been printed using the str( ) function. This is because we have instructed the program to view the string as an object by storing it in a variable. In order to print it in its original string form, it must be instructed to do so by passing the schmear objects as parameters through the str( ) function.

Membership

We are also able to sift through a string literal to locate specific characters and substrings. By using the membership operator called in, we’re able to signal to the program exactly what it needs to find. To examine how this works, let’s establish the string "supercalifragilisticexpialidocious" as an object named poppins then instruct the program to print a statement if the boolean value is truthy.

poppins = "supercalifragilisticexpialidocious"
if 'cali' in poppins:
  print ('Congrats, you get a spoon full of sugar!')

Congrats, you get a spoon full of sugar!
Plain text

Note: Capilatilization is key when using in. If the term “cali” had been referred to as “Cali”, the program would deem the term nonexisting and the print function would never run.

Because the keyword in returns a boolean value, it will only notify you if something is present. This means that despite the fact that the character “i” is present a total of seven times, the program will only acknowledge its presence by deeming it as truthy.

Alternatively, we can check whether a character or substring is not present. In true Python fashion, we would change in to not in.

This would result in the print statement not running at all because we would be telling the computer to print a statement if cali is not present.

poppins = "supercalifragilisticexpialidocious"
if 'cali' not in poppins:
  print ('Congrats, you get a spoon full of sugar!')
Plain text

Check Your Understanding

To recap, strings are a derived data type, consisting of a finite sequence of characters. We have discussed how they are created, how to join two or more together and how to identify the membership of specific data within strings. To check your understanding, take a look at the Vocabulary List, Python Operator Review, and Challenge Scenarios below.

Vocabulary Terms

  • String
  • In-line
  • Concatenate


Python Operator Chart

Challenge Scenarios

  1. What type of operators are **in** and **not in**? Hint: It’s a type of ‘belonging’.
  2. What is the name of the function you would use to return an object as a string?
  3. Marquis is programming an automated chat box for their school project using Python. When a user enters their name into the chat box, the bot will reply “Hi, { name }! Someone will be with you shortly.” If Marquis programmed { name } to be the only object within the print( ) function, which method did they use to concatenate everything together?