PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 5 - PYTHON STRINGS | WEEK 1
Good day everyone!
Hope you're doing great!! Today is the fourth day of the Python Programming class week 1. If you read my first introductory article for week 1, you will know our fifth topic for the day 5 and all we have to cover for day 5. Please if you have not read the first article please do well to read it here so that you will know what we have for each day of the week.
I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.
NB: This particular class will not be long as the teacher is not feeling too well.
Note: The English language used in this class is literally understandable by a layman. So let's begin...
Strings literals in python is any character enclosed in single or double quotation mark. 'Anyiglobal' and "Anyiglobal" are the same thing. You can output any string literal to the screen using the python print function.
For Example,
x = "Anyiglobal"
print(x)
Output
Anyiglobal
OR
print("Anyiglobal")
Output
Anyiglobal
NB: Copy the code and try this out in your text editor.
In python and other programming languages, strings are arrays of bytes representing unicode characters. Use this material or this material to learn more about unicode characters. Strings are also one of the data types in python. Characters in a string can be accessed using square brackets '[]'.
Accessing character in a string
We can access a string character using the following method in the code below.
For Example:
To access the second character 'n' in "Anyiglobal", we use [1]. Find the code below:
x = "Anyigloabal"
y = x[1]
print(y)
Output
n
NB: Try this out in your text editor.
Accessing characters in a string from one position to another position
You can as well access string from one position to another position.
For Example:
Let's access the characters in 'Anyiglobal' from position 4 to position 10 (not included). The index '10' is not included in the characters that will be printed to the screen. Check the example below, then copy the code and run it in your text editor. Try it for other positions! Learn more about python strings here.
x = "Anyiglobal"
y = x[4:10]
print(y)
Output
global
NB: Please note that the array index starts from 0. However, to access the characters from 'g' to 'l' we use [4:10] as written in the program above.
NB: Copy the code and try it in your text editor.
Accessing the last character in a string
To access the last character in a string use '[-1]'.
For Example,
x = "Anyiglobal"
y = x[-1]
print(y)
Output
l
NB: Try this out in your text editor. Learn more about python strings here.
String strip() method
strip() method is used to remove whitespaces in the beginning or the end of a string.
For example,
If you want to remove the whitespaces in the beginning and the end of the string " Anyiglobal " use the strip() method as written in the program below:
x = " Anyiglobal "
y = (x.strip())
print(y)
Output
Anyiglobal
Note that the output above does not include the whitespaces in the beginning and the end of string.
NB: Try the code in your text editor.
Checking the length of string
To check the length of string in python we use the 'len()' method.
For Example,
x = "Anyiglobal"
y = len(x)
print(y)
Output
10
From the code above, we were able to check the length of the given string. You can try this for different length of strings! Read more about python strings here.
NB: Copy the code above and try it out in your text editor.
Converting a string to lower case
To convert a string from uppercase to lowercase, use the 'lower()' method.
For Example,
x = "ANYIGLOBAL"
y = (x.lower())
print(y)
Output
anyiglobal
From the code above, we used the lower() method to convert the string "ANYIGLOBAL" from uppercase characters to lowercase characters.
Replacing a string character with another string character
To replace a string character with another string character use 'replace()' method.
For Example,
To replace the character 'H' with the character 'F' in the string "Hello, World!" use the following code below:
x = "Hello, World!"
y = (x.replace("H", "F"))
print(y)
Output
Fello, World!
In the code above, we replaced the character 'H' with the character 'F' in the string "Hello, World!" and we got our desired output in the terminal as "Fello, World".
NB: Make sure you try this out in your Vs code. Ensure that practice is the only way to learn programming fast. Read other people's articles also to have more understanding on what you're learning.
Splitting strings into substrings from a separator
We can split strings into substrings in python from an instance of a separator such as comma(,) , forward slash(/), e.t.c. Check the example below to understand what we mean.
For example,
x = "Hello, World!"
y = (x.split(",'))
print(y)
Output
['Hello', ' World']
Note that after the splitting, the result is returned as a list to us. We will learn about 'list' later in this course.
NB: Copy the code and try it out in your text editor.
Command-line Input
Python allows us to enter our input in the command-line during the program execution. This method accepts users input during program execution. The "input()" method is used to accept input from users during runtime.
For Example,
x = input("Please enter your first name: \n")
print("Your first name is " + x)
Output 1
Anyiglobal
Note that after entering your name, click 'Enter' for the print function to execute.
Output 2
Your first name is Anyiglobal
using the input() method
From the screenshot above, you can see in our program that we used the input() method to receive the first name of the user from the terminal.
NB: Copy this code and try it out in your text editor.
Read more about python strings here.
Conclusion
Python strings is very important to understand and manipulate as a python programmer. As we discussed in the class we can manipulate string characters using several python built-in methods. We learnt how to print python strings to the screen, how to access characters in a string, stripping whitespaces in a string, checking the length of a string, replacing string character with another character, converting string to a lowercase, and how to accept user input from the command-line.
This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed. Please I am so sorry that I didn't follow my course outline accordingly, please pardon me, it won't repeat itself in the week 2.
Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill. Tomorrow, we will look into "Python Operators". Visit here to see the topics we have for this week.
Thank you for sharing post and stay original.
Remark :
Thank you @anyiglobal for sharing this lesson with us.
I advise you refrain from taking too many users in a post as it won't reflect on any of the tagged accounts.
Pls remove campusconnect tag from your work as this is not a campus connect post.
Also try voting other authors to increase your voting CSI.
Thank you
Ok thanks for the information