Lesson 5: Working with Strings

  1. Create a String
  2. Accessing a single character in a String
  3. Code to access characters at different locations of a string
  4. Accessing multiple characters in a String
  5. Concatenating Strings
  6. Check if a character or group of characters exist in a string

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.


#Create a String
mystring = "computer"
print(mystring)
Notebook Output

2

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:

mystring[3]

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


#Get the first and last letter of the string
mystring = "computer"
print(mystring[0])
print(mystring[7])
print(mystring[-1])
Notebook Output


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:
mystring[3:5]
will be equal to “pu” because we started with position 3 and ended before position 5.

mystring[3:5]

You can also input just one position in the slice.

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

mystring[3:]

Code to access multiple characters:


mystring = "computer"
#Get all the letters in the string between
print(mystring[3:5])
#Get all the letters in the string starting at position 3
print(mystring[3:])
#Get all the letters in the string ending before position 3
print(mystring[:3])
Notebook Output

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.


greeting = "Hello "
name = "John"
print(greeting + name)
Notebook Output

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:


sample = "A43B23C83"
#check for a single character. Will return True if character is in the string
print("B" in sample)
#check for a group of characters
print("23C" in sample)
#will return false if not in the string
print("Z" in sample)
Notebook Output

Back to Menu