Hello guys welcome to my Python tutorial for beginners and in this post I'm going to show you how you can use string in your Python program efficiently.
So I have this Python file
and I have this Python shell open
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. 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
a = 'I am a single quoted string'
b = "I am a double quoted string"
c = """I am a triple quoted string"""
print(a)
print(b)
print(c)
We have three variables a, b, and c. And all three of them I have assigned a string and I'm trying to print
====================== RESTART: /root/Desktop/main6.py ======================
I am a single quoted string
I am a double quoted string
I am a triple quoted string
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
a = 'I am a single quoted string don't'
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. When I use the single quote, which with the string which is enclosed already in single code, then what your Python program will interpret is?
your string starts and your string ends here
and this will be some alien to your Python program, because it will 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 escape character, and what is the escape character in Python.
a = 'I am a single quoted string don\'t'
Escape character in Python is backslash, so just use this backslash and now you can see that you're backslash is hiding or it's saying Python that we want to use single code inside the string.
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
b = "I am a double "quoted string"
I want to use double quote, the same problem will come here,
you're a Python program will see that your string starts 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 use single quote here and you can use this backslash to show this backslash for or double quotes here
a = 'I am a single quoted string don\'t'
b = "I am a double \"quoted string"
c = """I am a triple \"quoted string"""
print(a)
print(b)
print(c)
Now I will save my program, I will just click ctrl s and run it once again, f5
====================== RESTART: /root/Desktop/main6.py ======================
I am a single quoted string don't
I am a double "quoted string
I am a triple "quoted string
and you can see the answer is like this.
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 “len”, 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
a = 'I am a single quoted string don\'t'
b = "I am a double \"quoted string"
c = """I am a triple \"quoted string"""
print( len (a) )
print(b)
print(c)
I can just use len and I will pass my variable a, as an argument
33
I am a double "quoted string
I am a triple "quoted string
and I can save it, and I can run it, I'll run it here once again and now it says 3. In the same way you can find the length of B and C also.
a = 'Hello '
b = "world"
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?
print (a + b)
I can join these strings just like you just need to use a, and plus and b. So plus is called a concatenation operator here and what this plus thats?, it just adds or concatenate these two string a, and b.
=================== RESTART: /root/Desktop/main6.py ======================
Hello world
but we'll save the program and run it once again and it gives me the answer hello world. 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
a = 'Hello '
b = "world"
print (a * 10)
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
====================== RESTART: /root/Desktop/main6.py ======================
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
and now you can see it has printed hello ten times. This we can do, you know we 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
a = 'Hello '
b = 5
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 five, and I want to print hello five in a single line, how can I do this? for example
a = 'Hello '
b = 5
print (a + b)
I can try to write it like this a plus b
====================== RESTART: /root/Desktop/main6.py ======================
Traceback (most recent call last):
File "/root/Desktop/main6.py", line 4, in modul
print (a + b)
TypeError: Can't convert 'int' object to str implicitly
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 data base.
Now in order to print this these two variables together, or value of these two variables together, what I can do is?
a = 'Hello '
b = 5
print (a + str(b))
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.
====================== RESTART: /root/Desktop/main6.py ======================
Hello 5
And now when I save this and run this, it will run fine hello five.
So in this way, you can use string in Python efficiently. I hope you have enjoyed this post.
Are you running root on your Desktop? :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
as you can see
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit