Generally a string is a collection of many letters together. In Python, strings are stored as a sequence of many letters that are individually stored on contiguous locations. In order to fetch a particular letter from the string, we are given two different kinds of indexes:
Look at the diagram:
Here we see that, for accessing a character from the string, we need to type:
For example, if we try to print 3rd character of the string site, we'd have to print site[2]
or site[-10]
and it'll produce 'R' on the console.
As we see that a string looks pretty similar to an array of letters stored in the name of a variable occupying contiguous memory locations, it's very important to know that using an assignment operator, we cannot replace or append a letter in a string. Look at the syntax below:
It'll produce an error because python doesn't allow us to insert or replace a letter in a string using indexes. Under python environment, strings are treated as an immutable data type.
Length of a string refers to the count of all characters in that string. We can find out the length of a string by calling a function named len(name_of_string)
. Look at the syntax:
The above code will print:
12
Traversing refers to going through all the elements of a string on after another thereby either accessing them or modifying them. In order to traverse through a string, a sequence based for loop can be created. Look at the syntax:
The above code will print:
o u r t u t o r i a l s
We can get the ASCII or Unicode value of a character by calling a function named ord(single_character)
. Look at the syntax:
The above code will print:
65
We can get the ASCII or Unicode value of a character by calling a function named chr(ASCII/Unicode_value)
. Look at the syntax:
The above code will print:
A
The +
operator has the ability to concatenate two or more strings. The only thing to be kept in mind is that it concatenates only when both its operands are strings. If both operands are numbers, it adds. It doesn't work if one is number and the other is a string. Look at the syntax:
The above code will print:
ourtutorials
The *
operator has the ability to replicate a string many times. The only thing to be kept in mind is that it replicates only when one of the operands is number and the other is a string. If both operands are numbers, it multiplies. It doesn't work if both the operands are strings. Look at the syntax:
The above code will print:
ourtutorialsourtutorialsourtutorials
It's also important to know that there is no any order of putting in the operands for replicating strings. In the above example it's 3*a
. We could write it as a*3
also and still the result would have been the same.
Membership operators are nothing but a pair of textual operators (in
and not in
) that verify whether a substring is there in the main string or not. These produce a boolean output i.e. TRUE or FALSE. Look at the syntax:
The above code will print:
TRUE
FALSE
Comparison operators are exactly the same that we've used in the name of realational operators i.e ==, !=, <,>,<=,>=
. Comparison among strings is done based on the ASCII value of characters in lexicographic (the way words appear in a dictionary) manner. Look at the syntax:
The above code will print:
FALSE because the first characters in both strings are in different cases.
TRUE because in a, 'p' is in small case hence having higher ASCII value compared to the 'P' in b.
FALSE because of the same reason why d is getting TRUE.
Slicing refers to extracting a specific part of the string. For this, we need a range of indices. We write the starting index and then the ending index separated by a colon. The starting and ending indices specify the start and end of the part that we want to extract. Look at the syntax:
Here we see that the range is given in a pair of square brackets and separated by colon.
Important is to keep in mind that python slices exactly one less than the ending index.
See the examples:
Note that str[0:3] is equivalent to str[-12:-9].
Note that str[3:] is equivalent to str[-9:].
Note that str[3:-4] is equivalent to str[-9:-4] or str[3:8]
Appending a -1
to the range of indices, reverses the sequence.
For example, if str[0:3]
extracts 'our'
, then str[0:3:-1]
will extract 'ruo'
.
This way we can also reverse the entire string just by the statement str[ : : -1 ]
.
Python provides, many functions related to strings. These functions are often called in relation to a string. To show this relation, we add a dot (.) between the string and the function. Look at the syntax:
capitalize(): This function is used to capitalize the string. The very first character of the string is capitalized. See the example:
The above code will print:
Ourtutorials is a content library.
isalnum(): This function checks and returns TRUE if the string is made-up of both alphabets and numbers. See the example:
The above code will print:
FALSE
isalpha(): This function checks and returns TRUE if the string is made-up of only alphabets. See the example:
The above code will print:
TRUE
isdigit(): This function checks and returns TRUE if the string is made-up of only numbers. See the example:
The above code will print:
TRUE
isspace(): This function checks and returns TRUE if the string is made-up of only white spaces. See the example:
The above code will print:
TRUE
islower(): This function checks and returns TRUE if all the characters in the string are in lower case. See the example:
The above code will print:
TRUE
isupper(): This function checks and returns TRUE if all the characters in the string are in upper case. See the example:
The above code will print:
TRUE
lower(): This function returns a copy of the string converted in lower case. See the example:
The above code will print:
ourtutorials
upper(): This function returns a copy of the string converted in upper case. See the example:
The above code will print:
OURTUTORIALS