Learn all the basics of python – Python beginners, let’s learn together (4)

Learning doesn’t stop, at any age!

Learning a little each day adds up!

In our last session, we learned what is variable and how to define and use it. In this session, I will show you how to use function to transform strings/variables.

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

Let’s try below code. The last line following yesterday’s code.

print(“I am XRKnows!”)
String1 = “I am XRKnows!”
print(“String1”)
print(String1)
String1 = “I am XRKnows and I am a pet lover!”
print(String1)
print(String1.upper())

What result do you see? Yes. All printed letters are in upper case now.

I AM XRKNOWS AND I AM A PET LOVER!

Here the upper() is a python function.

We can also ask python to change a string into lower cases.

String2 = “I AM ALL UPPER CASES!”
print(String2.lower())

The output will be:

i am all upper cases!

We can also check if one string is all lower cases. Try below code

print(String2.islower())

The output is:

False

This is because the String2 holds all upper-case letters: I AM ALL UPPER CASES!

Let’s try one more. We can also combine functions in one line of code.

print(String2.lower().islower())

We first use lower() function to convert the string into lower cases, when checking this with islower() function, the code returns; True.

True

We can also check how long is the string using len() function. Let’s try below code;

print(len(String2))

The output is 21! Because the String2 is holding: I AM ALL UPPER CASES! Notice that the spaces are counted as well!

We can also replace certain strings in the string variable with replace function. Try below code.

print(String2.replace(“ALL”, “21”))

The output is

I AM 21 UPPER CASES!

Great! Isn’t it?! Amazing. Let’s try another function, Index. Index function is used to find the location of one sub string in a string. Try below code.

print(String2.index(“P”,0,len(String2)))

This code returned 10. Note that if you count the string from 1, then it’s 11, however, remember Python index the string from 0. So, count from 0 to 10. This is the location of the first letter P appears in the string. Rember to count the space as well. I AM ALL UPPER CASES! One more thing to remember: the letter or string that you are searching for is case sensitive as well! If you put lower case p into above code, it will return ValueError: substring not found

Another cool thing to know. “\n”. n is the escape character in Python. It is mainly used to show the output on the new line. Let’s try below code

print(“I am XRKnows \nand I am a pet lover!”)

And the output is showing below. with the strings after \n moved to the next line.

I am XRKnows
and I am a pet lover!

This is all I learn from today!! Hope you will practice and memorize those!

Happy coding!! If you have any questions, feel free to leave me comments, let’s learn together!!

1 thought on “Learn all the basics of python – Python beginners, let’s learn together (4)”

  1. Pingback: Learn all the basics of python – Python beginners, let’s learn together (5) – XRKnows

Comments are closed.