DEV Community

Cover image for Python Strings for Beginners
Maggie
Maggie

Posted on • Edited on

Python Strings for Beginners

Strings are an integral data type used in multiple programming languages, including Python. You may or may not have heard of strings. Don’t worry, I’m here to help.

We’re going to go over:

  • What strings are

  • How to create strings

  • How to access characters in a string

  • How to find the length of a string

  • How to check if a character(s) is in a string

Let’s get started!

What are Python Strings?

Strings in Python are characters enclosed in quotes. That’s it. You can use single, double, or triple quotes! More on that in the next section.

Each character in the string is given a number called an “index” to represent its position within the string. The first character’s index is zero, the second character’s index is one, and so on.

Strings are immutable, which means you cannot edit strings once they’re created. In other words, an entire new string is created every time you add or delete a character within a string.

Creating Python Strings

There are three ways to create a string in Python. To create a string in Python, you surround the characters that make up the string using one the following.
You can use single quotes.

'Hello World'
Enter fullscreen mode Exit fullscreen mode

You can also use double quotes.

"Hello World"
Enter fullscreen mode Exit fullscreen mode

Lastly, you can use triple quotes to create a multi-line string.

"""Hello
World"""
Enter fullscreen mode Exit fullscreen mode

Accessing Characters in a String

As you work with strings, you may want to access one or multiple characters in a string. You can do so by using the index characteristic of strings.

To access one character in a string, use a technique called “indexing.” The format is as follows: the string variable is followed by square brackets that enclose the index of the character we’re trying to access.

Let’s say we have a string called name that holds the value Python, and we want to access the first letter. We do that by:

name = "Python"
print(name[0])
Enter fullscreen mode Exit fullscreen mode

This will return a new string with the result P.

To access more than one character in a string, use a technique called “slicing.” The format is the same as indexing, except you can include one to two numbers separated by a colon within the square brackets. The two numbers represent a slicing range; the first number represents the beginning index, and the second number represents the ending index. Both numbers can be positive or negative. Positive numbers represent the indexing of a string of left to right, whereas negative numbers represent the indexing of a string from right to left.

The following is a summary of the possible representations of how to slice a string:

  • string[beginningIndex:endIndex] returns the beginningIndex and up to but not including endIndex

  • string[:endIndex] returns the beginning of the string to but not including the endIndex

  • string[beginningIndex:] returns from the beginningIndex to the end of the string

Let’s use the same example from before, but say we want to access the first three letters in the string called name.

name = "Python"
print(name[0:3])
Enter fullscreen mode Exit fullscreen mode

This will return a new string with the result Pyt.

Two things to keep in mind is that:

  • If you include an index out of range, you’ll get an IndexError

  • If you include a non-Integer value as an index, you’ll get a TypeError.

Adding Strings Together

Adding strings together in Python is called concatenation. To concatenate strings together in Python, use the plus operator:

print("Hello " + "World")
Enter fullscreen mode Exit fullscreen mode

This will create a string with the value Hello World.

Finding the Length of a String

To find the length of a string, use Python’s built-in len() function:

print(len("Python"))
Enter fullscreen mode Exit fullscreen mode

This returns 6.

Checking if a Character or Substring Exists in a String

To check if a character or substring (part of the string) exists in a string, use the in keyword that’s built into Python. This will return True or False.

For example, if you want to see if “World” exists in “Hello World”:

print("World" in "Hello World")
Enter fullscreen mode Exit fullscreen mode

This returns True.

You can also iterate through a string and access each character. The most efficient way is to use a for loop. There are two ways to do this.

  • Use the in keyword within the for loop:
name = "Python"
for i in name:
    print(i)
Enter fullscreen mode Exit fullscreen mode

This code snippet will access and print each letter in the string from beginning to end. It outputs:

P
y
t
h
o
n
Enter fullscreen mode Exit fullscreen mode
  • Use the range() function in the for loop:
name = "Python"
for i in range(len(name)):
    print(name[i])
Enter fullscreen mode Exit fullscreen mode

This code snippet will access and print each letter in the string from beginning to end with a range of 0 to 5. It also outputs:

P
y
t
h
o
n
Enter fullscreen mode Exit fullscreen mode

Conclusion

There’s much more you can do with strings. Python has many built-in functions, like lower(), upper(), and split(). You can format your strings using something called escape sequences or the built-in format() function. So much fun!

Keep learning and comment below if you have any questions or comments!

Top comments (0)