Learn how to create, modify, and implement strings using the Python programming language.
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.
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.
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:
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.
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.
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.
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.
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.