Python Tutorial for Beginners 9 – Python Strings and Comments


Hey guys welcome to the next video in Python tutorial for beginners. In this video we will talk about using strings in python But before that I want to show you something And that is how to comment your code in python or how to write a comment in python So for example I have this line of code which is used to print hello world on terminal and for some reason I don´t want to execute it this line of code the I can convert this statement in to a comment and how do I do this? You can just add and asterisk in front your code and this makes your statement into a comment A comment means that now this line of code will not be executed by python So let´s run our code and let´s see what happens so you can see now it doesn´t print hello world because now this is a comment Generally we use comments in order to provide some information about some funtionality or what we want to do with our code So let me remove this line and because is a python strings video we can just write after this hast symbol python strings. right? and this means this is a comment and this line will not be executed by python interpreteur now there is one more way of giving a comment in python which is writting the double quotes three times and this is also a comment so when you write, for example, python strings this is also a comment so this also will be not executed by python interpreteur so what is the difference between this hast comment and the comment with this three double quotes so it turns out that using this double quotes you can write mutiple line comment So for example I want to write a multiple line comment let´s say I want to write python string tutorial I can write this multiple line comment using triple doble quotes So let´s get started and let´s see how we can use strings in python So I am going to declare a variable and this variable I am going to assign hello world.. (here, right?) and to print this hello world we can just write print and in the parentheses we can just use this ex variable and when we run the code, it runs we already know how to use this kind of strings in python the second way of creating a string is you can use single quote instead of double quotes this is also a valid string so this will also works so let me just a print this also which is hello world using single quotes and when I ..I am going to run this is going to give me the results Let me remove these spaces between the print and parentheses let´s say you want add a single quote into this hello world string so let´s say I want to just write hello single quote s, hello´s world so this is totally possible in python so whenever you soround your string using your double quote you can just add a single quote without any problem but when you do the same with the string this will give you a problem because now python thinks that your string starts from here and ends at this place just after hello and this will be seems as some kind of garbage by python so in order to use the single quote inside the string which use a single quote you use a scape character and a escape character in python is back slash, ok? so when you add a back slash in from of a character python interpreter knows that is a escape character so this will be teatred as a valid string so let me just print the result and you can see it prints the same result now let´s say I want to use a double quote in the string which uses a double quote already so I am going to give just a double quote here and the same problem arises which we have seen in the string with single code to escape this you can alse use back slash and the problem will be gone lets run the program and it gives us this hello world with double quote so you can print this double quote using this escape character which is a back slash now let´s say you want to print a string with a back slash itshelf so I can add a back slash here and I want to add a back slash here and let´s save the code and the lets run it and let´s see if it runs or not so it turned out that you can use this kind of notations in python also when you use double back slashes here and here let´s see what happens so when we run the code it will give us the same result because one back slash is a escape character and other back slash is the part of the string so it will print the same result so you can use this for a escaping this character or you can remove this, the result will be same so now let me add it this two strings and I am going to use hello here with the capital h and I am also going to assign hello to the y variable with a small h here now python provides us some inbuild funtions which you can use with strings so this is a string right? so you can use some methods related to strings on the x and y variable so let´s see what are these methods so after the x I can just press dot and you can see so many suggestions is given to us by pycharm and this is called intellegence with is provided by pycharm to us and in intelegence means than whenever you type something pycharm will try to suggest you some supporting code for whatever you are typing so let´s use some methods which are suggested to us so I am going to use this capitalize method let´s use this capitalize method with the x and y variable and let´s see what happen I am going to run the code and you can see now in the upper string is printed same but in the lower string is printed with a capital h in front of it so this capitalize method capitalizes the first letter of the string now let´s see what are some of the other methods which we can use so there is a method called upper and also there is a method called .lower so let me just rewrite this hello in all capitals and let´s run the code now and you can see in the result the upper method has converted our string in to all upper letters and the lower method has converted our string into all lower letters in python everything is an object so using this square brakets you can extract each and every letter based upon that index

11 Comments

Add a Comment

Your email address will not be published. Required fields are marked *