{"id":2762,"date":"2023-10-17T09:39:03","date_gmt":"2023-10-17T04:09:03","guid":{"rendered":"https:\/\/python-programs.com\/?p=2762"},"modified":"2023-11-10T11:44:43","modified_gmt":"2023-11-10T06:14:43","slug":"python-how-to-get-first-n-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-get-first-n-characters-in-a-string\/","title":{"rendered":"Python: How to get first N characters in a string?"},"content":{"rendered":"
This article is about finding the first N characters of String where the N represents any number or int value.<\/p>\n
As we know in Python String is a sequence of characters where the index numbers are associated with each character.<\/p>\n
For example : We have a String variable named Where the sequence number\/index of the first character starts with 0 and it goes on. Like<\/p>\n Index of character \u2018T\u2019 = 0<\/p>\n Index of character \u2018e\u2019 = 1<\/p>\n Index of character \u2018c\u2019 = 2<\/p>\n Index of character \u2018h\u2019 = 3<\/p>\n Index of character \u2018n\u2019 = 4<\/p>\n Index of character \u2018o\u2019 = 5<\/p>\n Index of character \u2018l\u2019\u00a0 = 6<\/p>\n Index of character \u2018o\u2019 = 7<\/p>\n Index of character \u2018g\u2019 = 8<\/p>\n Index of character \u2018y\u2019 = 9<\/p>\n 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.<\/p>\n Like String_value[i] will return i-th character of the string.<\/p>\n For example, String_value[4] will return character \u2018n\u2019.<\/p>\n Here, just by passing index position 0 in the operator [ ], we fetched the first character of the string.<\/span><\/p>\n Note : Like that by passing any index position in this [ ] operator, we can get the corresponding character of the string.<\/b><\/p>\n 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.<\/span><\/p>\n 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.<\/span><\/p>\n Where,<\/span><\/p>\n 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\u00a0<\/span><\/p>\n So, it will return characters starting from 0-th index upto n-1-th index. And here default Step_Size is 0.<\/span><\/p>\n So Let\u2019s do the program by using this substring concept.<\/span><\/p>\n 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 \u201cstring index out of range\u201d.<\/span><\/p>\n Let\u2019s overview the complete program again\u00a0<\/b><\/p>\n 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 : …<\/p>\nString_value<\/code> which contains a String Technology i.e<\/p>\n
String_value = \u201cTechnology\u201d<\/pre>\n
Program to get first character of the String :<\/h3>\n
#Program:\r\n\r\nString_value = \"Technology\"\r\nFirst_char = String_value[0]\r\nprint('First character of the string is', First_char)<\/pre>\n
Output : <\/b>\r\nFirst character of the string is T<\/span><\/pre>\n
Program to get first N characters of the String :<\/b><\/h3>\n
Syntax :String_VariableName [Start_IndexPosition : End_IndexPosition : Step_Size]<\/span><\/pre>\n
\n
String_value[0 : N]<\/span><\/pre>\n
#Program :\r\n\r\nString_value = \"Technology\"\r\nFirst_char = String_value[0:4]\r\nprint('First 4 characters of the string is', First_char)\r\n<\/pre>\n
Output :<\/b>\r\nFirst 4 characters of the string is Tech<\/span><\/pre>\n
Need to concern about Index Error :<\/strong><\/h3>\n
#Program :\r\n\r\nString_value = \"Technology\"\r\n#Passing 25th index which is not present in string\r\nFirst_char = String_value[25]\r\nprint('25-th character of the string is', First_char)\r\n<\/pre>\n
Output :<\/b>\r\nstring index out of range<\/span><\/pre>\n
#Program :\r\n\r\n#Asking for string input\r\nString_value = input()\r\n#Asking for a number i.e N-th value\r\nN=int(input())\r\n#This line prints first character of the string as index passed is 0\r\nFirst_char = String_value[0]\r\n#Printing the first character\r\nprint('First character of the string is', First_char)\r\n#It will find the substring from 0-th index to N-1-th index\r\nFirst_char = String_value[0 : N]\r\n#Printing the first N-th characters\r\nprint('First N-th character of the string is', First_char)\r\n<\/pre>\n
Output :<\/b>\r\nTechnology<\/span>\r\n4<\/span>\r\nFirst character of the string is T<\/span>\r\nFirst N-th character of the string is Tech<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"