- Create a String
- Accessing a single character in a String
- Code to access characters at different locations of a string
- Accessing multiple characters in a String
- Concatenating Strings
- 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)
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:
#Get the first and last letter of the string mystring = "computer" print(mystring) print(mystring) print(mystring[-1])
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.
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.
mean give me all the characters in the string starting at position 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])
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)
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)
- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling