This article is about finding the first N characters of String where the N represents any number or int value.
How to get the first N characters in a String using Python
As we know in Python String is a sequence of characters where the index numbers are associated with each character.
For example : We have a String variable named String_value
which contains a String Technology i.e
String_value = “Technology”
Where the sequence number/index of the first character starts with 0 and it goes on. Like
Index of character ‘T’ = 0
Index of character ‘e’ = 1
Index of character ‘c’ = 2
Index of character ‘h’ = 3
Index of character ‘n’ = 4
Index of character ‘o’ = 5
Index of character ‘l’ = 6
Index of character ‘o’ = 7
Index of character ‘g’ = 8
Index of character ‘y’ = 9
In Python, with the help of the [ ] (Subscript operator or Index Operator) any character of the string can be accessed just by passing the index number within it.
Like String_value[i] will return i-th character of the string.
For example, String_value[4] will return character ‘n’.
Program to get first character of the String :
#Program: String_value = "Technology" First_char = String_value[0] print('First character of the string is', First_char)
Output :
First character of the string is T
Here, just by passing index position 0 in the operator [ ], we fetched the first character of the string.
Note : Like that by passing any index position in this [ ] operator, we can get the corresponding character of the string.
But next we have to find first N characters simply means a substring we have to return from the original string. So lets know how to achieve that.
Program to get first N characters of the String :
In the above example we just passed a single index position inside the subscript operator, beside this, the subscript operator can also take a range too.
Syntax :String_VariableName [Start_IndexPosition : End_IndexPosition : Step_Size]
Where,
- Start_IndexPosition : It represents the index position from where it will start fetching the characters. (Here the default value is 0)
- End_IndexPosition : It represents the index position upto which it will fetch the characters.
- Step_Size : It represents the step value means interval between the characters. (Here the default value is 0)
So, to get the first N characters of the string, we have to pass 0 as start index and N as the end index i.e
String_value[0 : N]
So, it will return characters starting from 0-th index upto n-1-th index. And here default Step_Size is 0.
So Let’s do the program by using this substring concept.
#Program : String_value = "Technology" First_char = String_value[0:4] print('First 4 characters of the string is', First_char)
Output :
First 4 characters of the string is Tech
Need to concern about Index Error :
When we are using index operator [ ] , we need to be careful about the index that we are passing. Because if we pass the index which does not exist in the string then it will give an error “string index out of range”.
#Program : String_value = "Technology" #Passing 25th index which is not present in string First_char = String_value[25] print('25-th character of the string is', First_char)
Output :
string index out of range
Let’s overview the complete program again
#Program : #Asking for string input String_value = input() #Asking for a number i.e N-th value N=int(input()) #This line prints first character of the string as index passed is 0 First_char = String_value[0] #Printing the first character print('First character of the string is', First_char) #It will find the substring from 0-th index to N-1-th index First_char = String_value[0 : N] #Printing the first N-th characters print('First N-th character of the string is', First_char)
Output : Technology 4 First character of the string is T First N-th character of the string is Tech