Mastering String Manipulation in Python: An Introduction to Strings and String Slicing
Strings
Strings in python are defined by enclosing with either single quotation marks (") or double quotation marks (")
It does not make a difference in the way string is stored and/or used in the program based on the way it is declared (i.e., by using single quotation marks or double quotation marks).
Multi-line strings
Above declaration is applicable when the string is of a single line. If we have a string that has multiple lines, multi-line string can be defined by enclosing with either three single quotation marks (''') and double quotation marks (""").
Accessing part of the string by using index
String is similar to an array of characters (there is no character data type in Python) and Index starts from 0.
Similarly data can be accessed using negative index (i.e., -1 for the last position)
Below are the index positions of a string from above example.
H e l l o . . .
0 1 2 3 4 5 6 7
-8-7-6-5-4-3-2-1Looping through the string
We mentioned that the string is like an array of characters and we can access the specific character using it's index.
So, how to loop through all the characters of a string? There are two ways to achieve this.
- Retrieve string length and loop through the range and access character by using index.
- Loop through the string directly.
So, How to calculate the length of the string? 'len()' function returns length of a string.
In this example, for loop repeats for every character in the string and character can be accessed by the variable specified (char in this example) through out the loop.
What is string slicing?
By using the index we can only access one character at a time. We can use slicing to retrieve portion of the string.
For slicing, we need to provide the starting index (included) and ending index (not included in result).
In the above example (Line - 7), we are passing index '0' (as the starting position) and index '5' (as the ending position, not included) which returns the part of the string 'Hello'. Value in the index 5 (' ') is not included in the result.
In Line - 8, we are passing index '2' and '5', which would return the portion of the string from 2 to 4 (both included).
In the cases like Line - 7 where we are retrieving the porting of the string from beginning till the specified index, we won't really need to include index '0'.
Leaving the start index would consider the string from the beginning.
Similarly if we leave off end index, it would return the string from start index till end.
So, what happens if we don't mention both start and end index? It returns the full string.
Check if a character/string present in the string
We have seen how to access a character or a portion of string from a string. Now, Can we check if a character or a string is part of the string or not? Yes, we can do this by using 'in' and 'not in'.
In the above example,
- Line - 2: We are checking if 'H' is in the string, which returns True if present and False if not present.
- Line - 3: We are checking if 'H' is not in the string, which returns False if present and True if not present.
Similarly, we can use a string to check if it is part of another string.
It is highly unlikely we use 'in' and 'not in' operator the way we have described (this is only for better understanding).
Most likely we would be using this in a condition.
We have seen how to define a string & multi-line string, accessing a portion of the string (string slicing) and checking if a character or string is part of the string or not. Hope this has been a bit of help in understanding the use of strings in Python.
If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.
Comments
Post a Comment