Python Working with Strings

Buildandteach: Henry Palma
2 min readFeb 17, 2022


Strings are text that you want to display or use in your program. The are very popular in Python. To create a string simply create a variable and make it equal to some text surrounded by either single quotes or double quotes:

  • mystring = “computer”
  • mystring = ‘computer’

Each character in a string has a position in the string. Positions start with the number 0. For example, in the string “computer”, the character “c” occupies position 0 of the string and “r” occupies position 7. The last character can also be represented with the number -1.

Here is a graphical representation:

1) Create a String

To create a string simply create a variable and make it equal to your string in quotations. Quotations can either be single quotes or double quotes.

2) Accessing a single character in a String

You can access any character in the string simply by referring to its position. If I wanted the character “p” I would simply write the following code:

3) Code to access characters at different locations of a string:

4) Accessing multiple characters in a String

You can also use slice notation to get characters in the string. Slice notation specifies a range of positions. It is written using square brackets [start position (inclusive):end position (exclusive)]. When writing using slice notation you should think the following. Given mystring[3:5], give me the characters in the string mystring starting at position 3 and ending before position 5.

For example:
will be equal to “pu” because we started with position 3 and ended before position 5.

You can also input just one position in the slice.

For example:
mean give me all the characters in the string starting at position 3

Code to access multiple characters:

5) Concatenating Strings

Have you ever logged into an application that greeted you by name? This is an example of concatenated strings. The application has a string with a canned greeting “Hello “ and also a string with your name. To create the full greeting it must combine the canned greeting string with your name string.

In Python combining the string is simple. You just need to use the “+” operator.

6) Check if a character or group of characters exist in a string

To check if a character or group of characters exists in a string use the “in” keyword. For example to see if the string “A43B23C83” contains the character “B” you would use the following code: