Selecting subsequence from Python String
In this lecture, we will discuss “how to select subsequence from stringâ€. In the previous lecture, we learn “what is string and concatenation. We can repeat some previous concepts of the string as follows:
How to select subsequence in a string?
We can select subsequence in a string by the index of string. Index of string starts from 0. Let we take some example
Example:1
name = "friend"
print (name[4])   #we can select “n†alphabet in the given string through it index.
Output: Â n
The output of the above program is n because ‘n’ exists at 4 indexes. We can also select more alphabets through its index. Like we take some more example
Example:2
name = "friend"
print (name[1])   # Now we select ‘r’ alphabet in the following the string.
Output: r
At index 1 there exist “ r †alphabet that is accessible by the following statement. In the same way, we can access different alphabet by the different index of string.
Example:3
name = "friend"
print (name[8])Â Â Â Â Â Â Â Â Â Â Â #index 8 of string is out of range.
Output: Index is out of range.
The index is out of range because following index (name[8]) is not exist in the string. That’s why following output of the program is “Index is out of rangeâ€.
Reverse count of Index in the string
The index is also counted from right to left with minus sign. An example is given below
Example:4
[mks_pullquote align=”left” width=”620″ size=”15″ bg_color=”#74d62f” txt_color=”#ffffff”]Note: Now, we can print the “ e †alphabet in the given string. We will start counting from right to left. According to it “e†alphabet exist at -3 indexes in the given below example.[/mks_pullquote]name = "friend"   # also count index from right to left with minus sign.
print (name[-3])
Output: e
We can also count index of string from right to left. That’s why ‘e’ keyword exists at -3 index.
How to Print Multiple Characters in a String?
We will print multiple alphabets in a string.
Follow this syntax to print, multiple alphabets in a string
[start position: ending position+1 ]
Starting and Ending position separated by the colon(:). Let we take an example
Example:5
name = "friend"       # we print “ien†in the friend keyword
print (name[2:5])Â Â Â Â Â Â Â Â Â Â Â Â #print through start and ending position
Output:Â ien
Print specific alphabets from a string by starting and ending Position + 1 that are separated by the colon (:).
Reverse index of String (select right to left)
We can select string with the help of reverse index (right to left) of string. Let we take an example to clarify.
- [start position: ending position +1]Â from right-to-left
Example:6
name = "friend" # we can print “frie†keyword by selecting right to left method
print (name[ :-2])Â Â Â # starting position is not mentioned, then print all alphabet tell the end.
Output:Â Â Â Â Â frie
The output is “frie†because we are selecting subsequence from the string (right to left). If starting position is not mention in the statements then it will print all the alphabets tell the end. We can also print alphabets from the string by using left to right technique. Let we take an example
- [starting position:Â ]
Example: 7
name = "friend"  # now, we will print all the string without “frâ€
print (name[2: ])Â Â Â Â Â Â Â # if ending point is not mentioned it will print all the string tell the end.
Output: Â Â iend
Example:8
name = "friend"Â Â #now we will print all alphabets
print (name[0:3]+name[3: ])Â Â Â # print all the string by concatenation
Output:Â friend
In this lecture, we learn “Selecting subsequence from a stringâ€. I hope, it is a very helpful article. If you have any question then you can discuss in the comments section. You can also learn about HTML5, CSS3, PHP7 and more. Please, also give your feedback. Thank you!.