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
name = "friend" print (name)Â Â Â #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
name = "friend" print (name)Â Â Â # 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.
name = "friend" print (name)Â Â Â Â Â Â Â Â Â Â Â #index 8 of string is out of range. Output: Index is out of range.
The index is out of range because following index (name) 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
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
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:Â ]
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
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!.