Python Tutorial for Beginners 6 - Strings


hello guys welcome to the sixth blog tutorial on Python programming for beginners and in this blog I'm going to show you how you can use string in your Python program efficiently so I think I have shown you in the earlier blogs how to use strings very briefly but in this blog I'm going to take the string in detail so I have this Python file I have shown you in the last 
Blog that how you can make Python files and run them using this run command right so I have this Python file and I have this Python shell open so I'm going to write my code here and I will see the result in this shell okay so how to use strings in Python so there are three method or three ways you can use strings and I'm going to show you all three ways so consider this example we have three variables a B and C and all three of them I have assigned a string 1 string 4 which is assigned to variable a is enclosed in single quotes right the second string is enclosed in double goals right and the third string which is assigned to C is enclosed in 3 or triple code and I'm trying to print all three of them one by one so I want to print a print B and print C and let's see what happens first of all I'm going to save my program and then I will just run this module or press f5 to run it and when I press this one I will see the result here and the result will be the same so you can print the same string in three ways either you can use single code or you can do or you can use a double quotes or you can use triple codes to print the string and the result will always be seen in Python now suppose you want to use for example this is a string enclosed in single quote and you want to use some single code here so for example I want to say I'm a single quoted string and for example I want to use a word called the don't which also take a single quote here this kind of single quote and when I use the single quote which with the string which is enclosed already in single code then what you what your Python program will interpret is your string starts from here and your string ends here and this will be some alien to your fightin you know Python program because it will think that this single quote is start from here and ends here so this is a string and what is this it's not recognized by the Python so whenever you want to use a single quote in the string which is already there inside the single quote you can use our escape character and what is the escape character in Python escape character in Python is backslash so just use this backslash and now you can see that you're back backslash is hiding or it's saying Python that we want to use single code inside the string okay and this is called escape character so in the same way when you want to use double code in the string which is already there in the double quotes for example here I want to use double quote the same problem will come here you're a Python program will see that your string starts here and ends here and this will be not this will not be recognized by a Python program so here also you can use backslash to hide or to you know say Python that we are using double quotes but it's the part of the string and it's not starting or ending of the string okay in a same manner you can you know use single quote here and you can use this backslash to show this backslash for or double quotes here now I will save my program I will just click ctrl s and run it once again f5 and you can see the answer is like this so I'm a signal quoted string and this single code is visible now here and just remember that this is just an escape character so this will not be printed this will help your single code to print but this will not be printed in the same way you can see it's printing this double quote here because we are using escape character and in the same way it will work in the third string like this now suppose you want to print this this escape character so you have some string which uses this and you want to print here this backslash how you can do it you just need to use wherever you need to you know print this backslash for example I will use backslash here and let's see first of all what will be the result for example I want to print backslash here here and here and I will save my program and run it by pressing F or this run 5 module it will show me this backslash here right but better way is to just use double slash let's see what happens when we use double slash I will save it and run the program and now it shows backslash like this so it's always better to use double slash whenever you will want to print this single backslash here okay now suppose you want to print the length or you want to find out the length of your string what you can do you can use our built-in function called LEM and I think I have already you know shown this function to you but once again I am going to show you so I want to find the length of the string a I can just use LEM and I will pass my variable a as an argument and I can save it and I can run it I'll run it here once again and now it says 44 so the length of my string a is 34 okay so in the same way you can find the length of B and C also right now suppose I have a string a for example I have string a like this hello and space and string be is like that hello world okay and how can I join these strings I can join these strings just like you just need to use a and plus and be so plus is called a concatenation operator here and what this plus thus it just adds or concatenate these two string a and B but we'll save the program and run it once again and it gives me the answer hello worlds right so this plus concatenates your string you can see here now for example I have this I have this I will just delete this and I have this variable a and I want to print it ten times and I don't want to use you know a and then plus a once again a I want to print it ten times or suppose I want to print it hundred times there is a short way to print this a hundred times of whatever number of times what you need to do is you just need to use your variable which contains your string and then give a asterisk symbol here so give this asterisk symbol and then the number of time you want to iterate for example I want to print hello ten times I can just give ten here and I can save the program and I can run it once again and now you can see it has printed hello ten times this I can do you know I can change it to hundred and it will print hello hundred times just run it now you can see it has printed this hello hundred times right this is a short way of printing your same string again and again okay now suppose I have a string which is a so I have this board for example hello which is assigned to a and I have the other variable B and I assign a number to to this or five to this okay and I want to print hello five in a single line how can I do this I can for example I can try to write it like this a plus B but let's see what happens when I run this I will save it and I will run this and give me the error because my variable a is of type string and my variable B is of type integer and I cannot add or can concatenate these two variables because they B has a different data type and a has a different database now in order to print this these two variables together or value of these two variables together what I can do is I can typecast my B which is an integer to the string and to convert my integer to string what I can do is I can just do STR and I can enclose this B variable in this function called STR and what this STR does is it converts your argument into a string okay and now when I save this and run this it will run fine hello five right so in this way you can use string in Python efficiently I hope you have enjoyed this blog

Post a Comment

Previous Post Next Post

Recent in Technology News