[no audio]
Hi friends. Here
we are going to learn some of the string operations, they're
like you know case conversion.
I mean lower to upper, upper to lower, any string into title
format, or making capitalized, something like that. Okay.
Let me open my
[no audio]
Sublime Text Editor.
So guys here I'm going to use, you can use anything,
okay, but I am using Sublime Text Editor here.
Let me first give a name for that by pressing simply 'Ctrl + S',
so 'Desktop', then under our directory
let me create a 'case_conversion_operations'.
[no audio]
on_strings.py'.
[no audio]
That's it. Right. See guys
I am going to define a string called 'my_str', you can take
anything, okay. I'm going to write something like
"python scripting", okay.
Now let me print first our string directly
whatever you have. See the output.
Yes, you are getting. Now guys
I want to convert my entire string into lowercase.
Nothing is there, simply '.lower' operation, on your string '.lower' operation.
Or I want to convert my entire string into uppercase letters.
[no audio]
See the output.
Or after performing these two operations
I want to print my string as well.
See the output.
[no audio]
So here you have to understand one thing, whatever the operations
you are performing on your string with a '.', right,
they don't modify your original string.
That's why because of the third print statement
you are getting your original string Python scripting with
P&S only capital. Even though in second line you are performing
your string '.lower' and third line, right,
you're performing 'upper'
they don't modify your original string.
That's why strings are called 'immutable'. Once if you define
your string you cannot change the part of your string. Already
we know that. Already we have seen that, right. Fine. Suppose if
you want to capture after performing the lowercase letter
or after performing your entire string into 'upper' you can store
this operation result into some variable.
Let's say 'my_str', in lowercase only 'lower'. Any variable
you can take but I am taking this one. That's it.
Right. Now, instead of printing 'my_str.lower', already
we have done
on your variable. We perform 'lower' operation, and we stored
that result into this variable. Now print this variable and
see the output.
[no audio]
Let me comment these two lines and observe your output. Already
we have written, right. See the output. That's it.
Right. So guys as of now we have seen 'lower' and 'upper' operations
on your given string, but how you know how many operations
are there on a given string, right?
See, let me simply open my command line and let me enter into
my Python shell,
simply define any variable called any string, and simply run
'print(dir(X))', so 'dir' is the helping word
for us to list out all the operations which are available
on your given string because here 'X' is a string, because you
can randomly assign some string to some variable 'X', or any
variable, and on that variable if you perform simply 'dir(X)'
it will give the available operations on your given string.
There are so many, right, so I will go with step by step.
So now we have seen this is 'upper' operation, and somewhere you have
'lower' operation.
Here you have 'lower' operation.
Now, you have something called 'swapcase', 'title', 'capitalize'.
Let me go with the 'swapcase'.
Right. This 'swapcase' is nothing
but directly I'm going to print your result your string '.'
simply 'swapcase'.
See, observe the last output. What you are getting? lower
case into 'upper', uppercase into 'lower'. That's it.
But once again remember that whatever you are performing
'.' operation that is not going to disturb your original
string, only while printing it is modifying.
So if you want to capture 'swapcase' result, you can
store this result into some variable like your second line.
Right. So this is 'swapcase'.
[no audio]
Now guys, I want to print my string in terms of 'title'
format. So 'title' is nothing but printing each and every letter
of a word, starting letter of each and every word in terms of capital.
So already we have here 'capitalize'.
Let me remove this small letters "python scripting tutorials".
Now if I run the script and see the output for last 'print'
statement, you are getting "P" capital, "S" capital, "T" capital
because you are converting your given string into 'title' format.
'title' format is nothing but each and every, in each and
every word the starting letter should be capital.
That's what we are doing with the help of 'title'.
Right. Then 'capitalize'.
Let me give, let me take one more string called 'my_new_str',
you can take anything.
I'm taking simply small letters "python scripting".
[no audio]
So guys I want to make my string in terms of capitalized
format, right. See 'capitalize' format is nothing but observe
the output, you can understand. Only the starting letter in
a given string you are making it as a capital. That's it.
That is the format of 'capitalize', right, capitalized.
Fine. So guys,
these are some of the operations on your given strings
to convert one case to other case, or 'swapcase', or 'title', or
'capitalize', right.
So guys instead of your you know 'lower' you can also perform
one more operation called, I will do one thing,
only for 'lower' or 'upper',
you don't know whether given string is there in lowercase
or uppercase,
but the thing is you want to convert your entire string,
[no audio]
like 'swapcase'.
Let me do one thing,
'casefold'
[no audio]
'print', your new string '.casefold' and observe
the output what you are getting.
'my_new_str', yeah, I commented that, right?
Let me enable this. 'my_new_str', where it is? Yeah,
instead of 'my_new_str'
I will take 'my_str' variable. Anything. You can apply
on any string, right?
[no audio]
Observe the output.
[no audio]
Right. So you are converting your entire string, even though, let me
take here capital 'S'.
something called 'G'.
Any modification. See the output. What you are getting?
Right. You are getting all in terms of lowercase.
[no audio]
Fine. So instead of 'lower' operation
you can also perform 'casefold'.
Right. Fine. So guys,
these are some of the operations to convert your strings
from one case to other case, right?
Okay. Okay guys, thank you for watching this video.
[no audio]