In this post we will discuss some more methods
Lets open up the IDLE, and make a Sequence, like:
>>> python=['I', 'You', 'He', 'She', 'Me', 'They', 'Python']
>>> python
['I', 'You', 'He', 'She', 'Me', 'They', 'Python']
First, lets say, we want to add the element 'Idiot', between all the elements, for this, we need to assign the element to a variable.
>>> attach='Idiot'
>>> attach
'Idiot'
And to insert the element we need to follow this syntax:
to_be_attached_var_name.join(Listname_where_element_is_to_be_attached)
>>> attach.join(python)
Next lets say, we want to change all the characters in a string to lower case.
For this the syntax is:
variable_name.lower()
Ex:
>>> case="hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE"
>>> case
'hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE'
>>> case.lower()
'hi this is to check if python can convert this to lower case and upper case'
Okay we see here that everything is converted to lower case. For converting them to upper case, we need to replace the lower with upper.
Ex:
>>> case
'hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE'
>>> case.upper()
'HI THIS IS TO CHECK IF PYTHON CAN CONVERT THIS TO LOWER CASE AND UPPER CASE'
So we see here that everything is converted to Upper Case.
Next we will see how to replace an element,
>>> rep="People who hates Python, Python Hates Them"
>>> rep
'People who hates Python, Python Hates Them'
>>>
Lets say, we want to replace 'hates' with 'Loves'
For this we need to use the replace function, the syntax is:
listname.replace('what', 'with what')
>>> rep.replace('hates', 'Loves')
'People who Loves Python, Python Hates Them'
>>>
Whaat?? No, Python is not supposed to be that rude, Say what, we made a mistake here, we know python is case-sensitive, so for him, 'hates' is different from 'Hates'
So if we correct it we get:
Please Note the way I correct it, and try to understand, Don't give up on it, still if u face problem with this, feel free to comment.
>>> rep
'People who hates Python, Python Hates Them'
>>> rep=rep.replace('Hates', 'hates')
>>> rep
'People who hates Python, Python hates Them'
>>> rep=rep.replace('hates', 'Loves')
>>> rep
'People who Loves Python, Python Loves Them'
>>>
So Finally we are in a mutual understanding with Python, and made us love each other.
Hope, this was informative for you, practice this, and Don't forget to check my next post.
Thank You.
Lets open up the IDLE, and make a Sequence, like:
>>> python=['I', 'You', 'He', 'She', 'Me', 'They', 'Python']
>>> python
['I', 'You', 'He', 'She', 'Me', 'They', 'Python']
First, lets say, we want to add the element 'Idiot', between all the elements, for this, we need to assign the element to a variable.
>>> attach='Idiot'
>>> attach
'Idiot'
And to insert the element we need to follow this syntax:
to_be_attached_var_name.join(Listname_where_element_is_to_be_attached)
>>> attach.join(python)
'IIdiotYouIdiotHeIdiotSheIdiotMeIdiotTheyIdiotPython'
Next lets say, we want to change all the characters in a string to lower case.
For this the syntax is:
variable_name.lower()
Ex:
>>> case="hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE"
>>> case
'hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE'
>>> case.lower()
'hi this is to check if python can convert this to lower case and upper case'
Okay we see here that everything is converted to lower case. For converting them to upper case, we need to replace the lower with upper.
Ex:
>>> case
'hI tHiS iS tO cHeCk iF pYtHon cAn ConvErT tHiS tO lOwEr cAsE aNd uPpEr cAsE'
>>> case.upper()
'HI THIS IS TO CHECK IF PYTHON CAN CONVERT THIS TO LOWER CASE AND UPPER CASE'
So we see here that everything is converted to Upper Case.
Next we will see how to replace an element,
>>> rep="People who hates Python, Python Hates Them"
>>> rep
'People who hates Python, Python Hates Them'
>>>
Lets say, we want to replace 'hates' with 'Loves'
For this we need to use the replace function, the syntax is:
listname.replace('what', 'with what')
>>> rep.replace('hates', 'Loves')
'People who Loves Python, Python Hates Them'
>>>
Whaat?? No, Python is not supposed to be that rude, Say what, we made a mistake here, we know python is case-sensitive, so for him, 'hates' is different from 'Hates'
So if we correct it we get:
Please Note the way I correct it, and try to understand, Don't give up on it, still if u face problem with this, feel free to comment.
>>> rep
'People who hates Python, Python Hates Them'
>>> rep=rep.replace('Hates', 'hates')
>>> rep
'People who hates Python, Python hates Them'
>>> rep=rep.replace('hates', 'Loves')
>>> rep
'People who Loves Python, Python Loves Them'
>>>
So Finally we are in a mutual understanding with Python, and made us love each other.
Hope, this was informative for you, practice this, and Don't forget to check my next post.
Thank You.
0 comments:
Post a Comment