Python Strings

String is like a array of character. We can simple print a string by using double or single quotes(Both can work). Can also print a string by assigning it to a variable and then printing that variable. Now this was all about single line string but what about multiline string, how to handle it, Simple use double or single 3 quotes it would generate any error as mentioned below

As mentioned string is array of character so we can simply access any character of string using its index value which starts from ‘0’. Lets add the below mentioned line to our existing code.

Using len() method we can know the length of the string, lets add len(abc) method to our existing code to know the length of string assigned to variable abc.

To know if a particular set of character or phrase is present in our string or not, just use the below mentioned code. Here we used ‘in’ operator.

Similarly we can use ‘not in’ to find out a particular set of characters or a phrase is present in our string or not.

Using upper() and lower() method we can easily convert a string to upper and lower case respectively as mentioned below

If you want to cut the extra white space in the beginning or end of a string then simply use strip() method.

In situations where you want to replace a particular character of your string with some other character, this could be achieved by using replace() method. As demonstrated below

You can split the string into 2 parts by using split() method, just need to specify a partitioning character(as we used ‘i’ in our example).

Leave a Reply

Your email address will not be published. Required fields are marked *