Heyyyyy !!!, How is it going?? Today, we will be learning about some methods/functions to manipulate strings. These methods/functions can be important as you become more advanced, so try to keep up with me as I go through the major functions you can use to manipulate strings. Now what exactly do I mean by ‘manipulation of strings’?? You will find out in just a moment. So, let’s begin !!!
upper():
This
function converts every letter in the string to upper case, i.e. it converts
every letter into a capital letter. For example, ‘hello’ becomes ‘HELLO’ and
‘programmer’ becomes ‘PROGRAMMER’.
This
function converts the string into lower case. It is just the opposite of
upper(). For example, ‘NOOB CODE PRO’ becomes ‘noob code pro’ and ‘Alex’
becomes ‘alex’.
In both of
these functions, if the string is already in upper case (in case of upper()) or
lower case (in case of lower()), the function will return the string as it is, it will not raise any error.
capitalize():
This
converts the first letter of every word into upper case. This is a useful
function if you are trying to input someone’s name and you want to make sure
that the first later of his name and surname are in capital letters. Example:
‘alexander’ becomes ‘Alexander’.
format():
This
function looks for curly braces {} in a string and replaces it with the
parameters you have passed. OK guys, this function is of paramount importance.
I mean this is one of the ‘must know’ functions in Python, and it has a cool
use as well. What this basically does is if you want to print a certain text
which you are unknown of at the moment of writing the program, you can use this
function to add that text into the string as the program continues. For
example, if you take an input from the user and want to print it, you obviously
don’t know what the user is going to input, so you cannot print a pre-decided
number. Suppose, the user inputs ‘10’
and you store that value in a variable ‘x’. Now if you want to print that value
saying, ‘The value of x is 10’. So how do you add a variable value to your
string?? You use the format() method.
split():
This
function splits a string into two or more strings. You have to specify a
character in string format between the brackets. Whenever I say “in string
format”, I mean you must use colons to show Python that it is a string. Now the
function will split all the characters before the character you specified and
after the character you specified and return a list containing the resulting
strings.
For example
‘I am a self taught programmer. I love learning programming from Noob Code
Pro’.split(‘.’)
The split() function will now split all the
characters before the period and after the period and return a list containing
the two strings.
The character
that you specify between the brackets could be a space (“ “) as well.
join():
This is
another important function that lets you add a new character in between every
element of a string. For example, if you have ‘string1’ and if you want to have
a comma(,) in between each character, the line will be:
string2=’,’.join(string1)
We can also
use this function to join the elements in a list to create a string.
CONGRATULATIONS !!! You have learnt about manipulating strings in Python. Now you know awesome functions which you can use when working with strings in Python. Trust me this knowledge will help you create programs that are a lot easier to understand and execute. Have fun with it !!! Create some cool programs. Good luck !!!!
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER, IF IT DIDN'T FEEL FREE TO ASK ANY FURTHER QUERIES IN THE COMMENTS SECTION BELOW or IN THE TELEGRAM CHANNEL LINKED BELOW !!! .
0 Comments
Welcome to the comments section, this is where you can contact me personally for any doubts or feedback