{"id":10293,"date":"2021-09-30T12:00:50","date_gmt":"2021-09-30T06:30:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=10293"},"modified":"2021-11-22T18:34:40","modified_gmt":"2021-11-22T13:04:40","slug":"python-program-to-form-a-new-string-made-of-the-first-2-and-last-2-characters-from-a-given-string","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-form-a-new-string-made-of-the-first-2-and-last-2-characters-from-a-given-string\/","title":{"rendered":"Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n \u201cString is a character collection or array\u201d<\/strong><\/p>\n Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a string of length 1 if we write \u2018r’.<\/p>\n Given a string, the task is to form a new string which is made of the First 2 and Last 2 characters From a Given String in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the full approach to form a new string which is made of the First 2 and Last 2 characters From a Given String in Python.<\/p>\n Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Here string concatenation is used to combine the characters using string slicing and length of the string.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Here string concatenation is used to combine the characters using string slicing and length of the string. Strings in Python: \u201cString is a character collection or array\u201d Well in Python too, for the string data type, we say the same definition. The string is a sequenced character array and is written within single, double, or three quotes. Also, Python does not have the data type character, thus it is used as a …<\/p>\ngiven string =BTechGeeks<\/pre>\n
BTks<\/pre>\n
given string =AplusTopper<\/pre>\n
Aper<\/pre>\n
Program to Form a New String Made of the First 2 and Last 2 characters From a Given String<\/h2>\n
\n
1)Using indexing, Slicing, and Count Variable(Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngiven_strng = 'BTechGeeks'\r\n# Take a variable to say stringcharacters that stores the total characters in the given string.\r\n# Initialize the stringcharacters to 0.\r\nstringcharacters = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Increment the value of stringcharacters by 1.\r\n stringcharacters = stringcharacters+1\r\n# Using String Concatenation and slicing form the new string with which is made of the First two\r\n# and Last two characters From a Given String.\r\nresstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]\r\n# print the result string\r\nprint(resstring)\r\n<\/pre>\n
BTks<\/pre>\n
2)Using indexing, Slicing, and Count Variable(User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngiven_strng = input('Enter some random string = ')\r\n# Take a variable to say stringcharacters that stores the total characters in the given string.\r\n# Initialize the stringcharacters to 0.\r\nstringcharacters = 0\r\n# Traverse the given string using for loop.\r\nfor charact in given_strng:\r\n # Increment the value of stringcharacters by 1.\r\n stringcharacters = stringcharacters+1\r\n# Using String Concatenation and slicing form the new string with which is made of the First two\r\n# and Last two characters From a Given String.\r\nresstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]\r\n# print the result string\r\nprint(resstring)\r\n<\/pre>\n
Enter some random string = AplusTopper\r\nAper<\/pre>\n
\n
3)Using indexing, Slicing, and len() function(Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngiven_strng = 'BTechGeeks'\r\n# Calculate the length of the string using the len() function and store it in a variable.\r\nstringcharacters = len(given_strng)\r\n# Using String Concatenation and slicing form the new string with which is made of the First two\r\n# and Last two characters From a Given String.\r\nresstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]\r\n# print the result string\r\nprint(resstring)\r\n<\/pre>\n
BTks<\/pre>\n
4)Using indexing, Slicing, and len() function(User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngiven_strng = input('Enter some random string = ')\r\n# Calculate the length of the string using the len() function and store it in a variable.\r\nstringcharacters = len(given_strng)\r\n# Using String Concatenation and slicing form the new string with which is made of the First two\r\n# and Last two characters From a Given String.\r\nresstring = given_strng[0:2]+given_strng[stringcharacters-2:stringcharacters]\r\n# print the result string\r\nprint(resstring)\r\n<\/pre>\n
Enter some random string = AplusTopper\r\nAper<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n