In Chapter 7 of Python Tutorial, we will learn about Basic String Operations — Part 2. It is divided into two parts to make it easy for you to understand
Before we begin, if you are viewing this tutorial on Mobile, please rotate your mobile horizontally to view the tutorial for best experience. You can enable auto-rotate in the phone settings.
Welcome to Chapter 7 of Python Tutorial — Part 2 of Basic String Operations. We are continuing from Part 1 of Basic String Operations.
We have so far learnt about below string operations:
- Finding the length of a string
- Printing out the index number of a character
- Finding out the count of a character/word in a string
In this chapter 7, we will learn about the remaining string operations:
- Accessing characters by slicing method in a string
- How to reverse a string
- Convert letters in a string to uppercase or lowercase
- Finding out whether the string starts with/ends with a certain word or a character
- How to split a string
Accessing characters by slicing method in a string:
We can print out a range of characters from a string using slicing method. We need to specify start index and the end index, separated by a colon, to print out a part of the string. Please refer below example:
#print the characters from index 2 to index 7
chrslc = "Learning Slicing!"
print(chrslc[2:7])
This prints a slice of the string, starting from index 2 (letter “a”), and ending at index 6 (letter “n”). Even though, index 7 is mentioned in the slicing method, it is not included. Most programming languages follow this procedure because it is easier to perform operations.
We can play around slicing method with different operations. Refer below examples:
#slice from start index to index 7
chrslc = "Learning Slicing!"
print(chrslc[:7])
#Left out the first number so it started slicing from index 0
#slice from index 2 till the end index
chrslc = "Learning Slicing!"
print(chrslc[2:])
#Left out the second number so it will print out from index 2 till the end of the string
#slicing from end of the string instead of the beginning
chrslc = "Learning Slicing!"
print(chrslc[-4:-1])
#Using negative indexing to start index from end of the string instead of beginning
To print characters by skipping 1 or 2 characters in between, we use [start:stop:step] method. It is one of the slicing method. Please refer below example:
#print the characters by skipping one or two characters in between
chrslc = "Learning Slicing!"
print(chrslc[2:7:2])
#This prints the characters of string from 2 to 7 skipping one character
How to reverse a string:
We can reverse a string using the below slicing method:
#reverse the string
chrslc = "Learning Slicing!"
print(chrslc[::-1])
Convert letters in a string to uppercase or lowercase:
Let’s convert characters in a string from lowercase to uppercase and vice-versa. Refer below example:
#Convert characters from uppercase to lowercase and vice-versa
chrslc = "Learning Slicing!"
print(chrslc.upper()) #converts to uppercase
print(chrslc.lower()) #converts to lowercase
Finding out whether the string starts with/ends with a certain word or a character:
If you would like to find out whether a particular string is starting with a certain word/character or if it is ending with a certain word/character, then you can use below string operations. It will print out True if it is matching the character/word or it will print out False if there is no match:
#startswith, endswith
strop = "Hello Python!"
print(strop.startswith("testing"))
#Prints out False since string doesn't start with "testing"
print(strop.endswith("Python!"))
#Prints out True since string ends with "Python"
How to split a string:
We can split a string into a list of strings by using the split() function, which breaks the given string using the designated separator.
#split the string
str= "Split the String!"
splstr = str.split(" ")
print(splstr)
In this example, split is happening at a space. Hence, the first element in the list will be “Split”, the second will be “the” and the third will be “String!”.
That brings to the end of chapter 7 of Python tutorial, see you in the next tutorial!!!
Assignment:
Please write code to perform various string operations on the string provided below:
#Perform string operations on below string
teststr = "This is test string!"
#Find the length of the string
#Find first occurrence of "s"
#Find number of s's
#Print first five characters of the string using slicing method
#Print the seventh character of the string using slicing method
#Print last five characters of the string using slicing method
#Convert everything to uppercase
#Convert everything to lowercase
#Check if string starts with "This"
#Check if string ends with "Ends"
#Split the string using the separator
#Perform string operations on below string
teststr = "This is test string!"
#Find the length of the string
print(len(teststr))
#Find first occurrence of "s"
print(teststr.index("s"))
#Find number of s's
print(teststr.count("s"))
#Print first five characters of the string using slicing method
print(teststr[:5])
#Print the seventh character of the string using slicing method
print(teststr[8])
#Print last five characters of the string using slicing method
print(teststr[-5:])
#Convert everything to uppercase
print(teststr.upper())
#Convert everything to lowercase
print(teststr.lower())
#Check if string starts with "This"
print(teststr.startswith("This"))
#Check if string ends with "Ends"
print(teststr.endswith("Ends"))
#Split the string using the separator
print(teststr.split(" "))
By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install/configure Python and Pycharm IDE on Windows OS.
How to Install Python 3 and Pycharm IDE on Windows
How to configure Pycharm IDE and “Run” your first Python 3 program
For your reference, to download Pycharm IDE, visit below link:
https://www.jetbrains.com/pycharm/download/?section=windows
If you have any questions or comments about chapter 7 of Python tutorial, please let us know in the comments section below.