In Chapter 6 of Python Tutorial, we will learn about Basic String Operations — Part 1. It is divided into two parts to make it easy for you to understand
Welcome to Chapter 6! By now, you must be aware about strings in Python. Strings are text defined between the quotes. We have already seen “Hello World” or “Hello Python” examples.
#string in double quotes strex1 = "Hello Python" #string in single quotes strex2 = 'Hello Python'
As you can see, we assigned double quotes and single quotes to the strings. Both are correct, however, it is advisable to use double quotes to avoid any syntax error. We have already shown you an example in Chapter 2 of Python tutorial.
#text with single quote which has apostrophe
apstr = 'Code with single quotes and apostrophe can't execute'
print(apstr)
Click on “Run” to execute the code. You will run into syntax error.
Now, make changes to the code by removing single quotes and replacing them with double quotes. Once done, click on “Run”. This time code should be executed successfully.
Moving on in Chapter 6 of Python Tutorial, we will learn about various string operations such as:
- 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
- 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
Finding the length of a string:
To find the length of string, we use len() function. Please check the example below:
#length of string
lenstr = "Printing length!!!"
print(len(lenstr))
That prints out 18, because “Printing length!!!” is 18 characters long, including punctuation and spaces. Try your own strings in the IDE above and find out the length.
Printing out the index number of a character:
To find out the index number of a character, we use index() function. Please refer below example:
#index number of character
indchr = "index number"
print(indchr.index("n"))
This will print out 1. The reason is because in the string “index number”, the first occurrence of the letter “n” is 1. To give a clear idea on how index number is calculated in Python, refer the below screenshot:
Please note that the index number starts at 0 instead of 1. To keep things simple, most of the programming languages including Python follow this process. So, the index of “n” is 1 instead of 2.
Also, if you notice, there are actually two n’s in the phrase, but it gave the index number of first occurrence only. This method only recognizes the first.
Finding out the count of a character/word in a string:
To find out the count of characters/word, we use count() function. Please refer below example:
#count of character/word
chrct = "count characters"
print(chrct.count("c"))
#count of word
print(chrct.count("count"))
The number of c’s in the string is 3 so it should print out 3. Also, the number of word “count” is 1 so it will print out 1. Give it a try, click on “Run”. Try your own strings in the same IDE above and find out the count of characters/words in your string!
That brings to the end of chapter 6 of Python tutorial, see you in the next tutorial!!!
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 6 of Python tutorial, please let us know in the comments section below.