Skip to main content

How To Code in Python 3: An Introduction to Working with Strings

How To Code in Python 3
An Introduction to Working with Strings
    • Notifications
    • Privacy
  • Project HomeHow To Code in Python 3
  • Projects
  • Learn more about Manifold

Notes

Show the following:

  • Annotations
  • Resources
Search within:

Adjust appearance:

  • font
    Font style
  • color scheme
  • Margins
table of contents
  1. Introduction
  2. Python 2 vs Python 3: Practical Considerations
  3. How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 16.04
  4. How To Install Python 3 and Set Up a Local Programming Environment on macOS
  5. How To Install Python 3 and Set Up a Local Programming Environment on Windows 10
  6. How To Install Python 3 and Set Up a Local Programming Environment on CentOS 7
  7. How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 16.04 Server
  8. How To Write Your First Python 3 Program
  9. How To Work with the Python Interactive Console
  10. How To Write Comments
  11. Understanding Data Types
  12. An Introduction to Working with Strings
  13. How To Format Text
  14. An Introduction to String Functions
  15. How To Index and Slice Strings
  16. How To Convert Data Types
  17. How To Use Variables
  18. How To Use String Formatters
  19. How To Do Math with Operators
  20. Built-in Python 3 Functions for Working with Numbers
  21. Understanding Boolean Logic
  22. Understanding Lists
  23. How To Use List Methods
  24. Understanding List Comprehensions
  25. Understanding Tuples
  26. Understanding Dictionaries
  27. How To Import Modules
  28. How To Write Modules
  29. How To Write Conditional Statements
  30. How To Construct While Loops
  31. How To Construct For Loops
  32. How To Use Break, Continue, and Pass Statements when Working with Loops
  33. How To Define Functions
  34. How To Use *args and **kwargs
  35. How To Construct Classes and Define Objects
  36. Understanding Class and Instance Variables
  37. Understanding Inheritance
  38. How To Apply Polymorphism to Classes
  39. How To Use the Python Debugger
  40. How To Debug Python with an Interactive Console
  41. How To Use Logging
  42. How To Port Python 2 Code to Python 3

An Introduction to Working with Strings

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging.

Because text is such a common form of data that we use in everyday life, the string data type is a very important building block of programming.

This Python tutorial will go over how to create and print strings, how to concatenate and replicate strings, and how to store strings in variables.

Creating and Printing Strings

Strings exist within either single quotes ' or double quotes " in Python, so to create a string, enclose a sequence of characters in one or the other:

'This is a string in single quotes.'
"This is a string in double quotes."

You can choose to use either single quotes or double quotes, but whichever you decide on you should be consistent within a program.

We can print out strings by simply calling the print() function:

print("Let's print out this string.")
Output
Let's print out this string.

With an understanding of how strings are formatted in Python, let’s take a look at how we can work with and manipulate strings in programs.

String Concatenation

Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

Let’s combine the strings "Sammy" and "Shark" together with concatenation through a print() statement:

print("Sammy" + "Shark")
Output
SammyShark

If we would like a whitespace between the two strings, we can simply include the whitespace within a string, like after the word “Sammy”:

print("Sammy " + "Shark")
Output
Sammy Shark

Be sure not to use the + operator between two different data types. We can’t concatenate strings and integers together, for instance. So, if we try to write:

print("Sammy" + 27)

We will receive the following error:

Output
TypeError: Can't convert 'int' object to str implicitly

If we wanted to create the string "Sammy27", we could do so by putting the number 27 in quotes ("27") so that it is no longer an integer but is instead a string. Converting numbers to strings for concatenation can be useful when dealing with zip codes or phone numbers, for example, as we don’t want to perform addition between a country code and an area code, but we do want them to stay together.

When we combine two or more strings through concatenation we are creating a new string that we can use throughout our program.

String Replication

There may be times when you need to use Python to automate tasks, and one way you may do this is through repeating a string several times. You can do so with the * operator. Like the + operator, the * operator has a different use when used with numbers, where it is the operator for multiplication. When used with one string and one integer, * is the string replication operator, repeating a single string however many times you would like through the integer you provide.

Let’s print out “Sammy” 9 times without typing out “Sammy” 9 times with the * operator:

print("Sammy" * 9)
Output
SammySammySammySammySammySammySammySammySammy

With string replication, we can repeat the single string value the amount of times equivalent to the integer value.

Storing Strings in Variables

Variables are symbols that you can use to store data in a program. You can think of them as an empty box that you fill with some data or value. Strings are data, so we can use them to fill up a variable. Declaring strings as variables can make it easier for us to work with strings throughout our Python programs.

To store a string inside a variable, we simply need to assign a variable to a string. In this case let’s declare my_str as our variable:

my_str = "Sammy likes declaring strings."

Now that we have the variable my_str set to that particular string, we can print the variable like so:

print(my_str)

And we will receive the following output:

Output
Sammy likes declaring strings.

By using variables to stand in for strings, we do not have to retype a string each time we want to use it, making it simpler for us to work with and manipulate strings within our programs.

Conclusion

This tutorial went over the basics of working with the string data type in the Python 3 programming language. Creating and printing strings, concatenating and replicating strings, and storing strings in variables will provide you with the fundamentals to use strings in your Python 3 programs.

Continue learning more about strings by taking a look at the following tutorials: - How To Format Text in Python 3 - An Introduction to String Functions - How To Index and Slice Strings - How To Use String Formatters

Annotate

Next Chapter
How To Format Text
PreviousNext
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
Powered by Manifold Scholarship. Learn more at
Opens in new tab or windowmanifoldapp.org