In Python, Strings are arrays of bytes representing Unicode characters. In simple words, anything that can be described inside single, double, or triple inverted quotes is called String.
Creating A String
Indexing in String
Index is the position which can be used to get a specific Character from the String.
Here, Indexing in String starts from 0, then 1 , then 2 , then 3 , then 4 and so on. and if we start from last index of list, then indexing starts from -1, then -2 , then -3 ….so on.
Using Index , we can retrieve any character from the String .
Now, Element located at nth index can be find by writting list name followed by square brackets, and inside square bracket, we have to write the index for which we want character corresponding to that index.
String Slicing is the process to make SubString from a given String.
Example : Let’s suppose , there is a String say myString = “BMPL” , and we have to make a sub String from it by taking out the first 10 characters from given String..
Syntax for String Slicing : Stringname [start index : end index : step size]
Reverse of String
String can be reversed by traversing from last ,this can be done using step size (-1) . Here , we will start from last index , then with step size of -1, we got indexes like -1,-2,-3,-4,-5 ….so on. and finally we will get our reversed String.
- lower() :This is used to convert all uppercase characters(Alphabeticals) of String in lowercase.
- upper() :This is used to convert all lowercase characters(Alphabeticals) of String in uppercase.
- capitalize() :This is used to convert the first character of String to upper case.
- title() :This is used to convert the first character of each word to upper case.
- split() :This is used to Splits the string at the specified separator, and returns a list
- count() :This is used to count the frequency of the specific character in a String.
- strip() :This is used to returns a cropped version of the string.
- lstrip() and rstrip():This is used to returns a left cropped and right cropped version of the string. respectively.
- startswith() & endswith() :
startswith() returns True if String starts with a specific pattern otherwise returns false,
endswith() returns True if String ends with a specific pattern otherwise returns false
- center():This is used to returns a centered String.
- index():This is used to find the index of specific character in a String. and if character doesn’t exists inside String. Then it will return -1.
- ord():This will return the ASCII value of that character which has been passed inside function parenthesis.
- chr():This will return the character for the ASCII value that we passes inside chr() function.