Python - Create a string made of the first and last two characters from a given string Last Updated : 14 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To solve the problem, we need to create a new string that combines the first two and the last two characters of a given string. If the input string is too short (less than 2 characters), we should return an empty string. This task is simple and can be achieved using a variety of methods.Using slicingThis method uses Python's slicing feature to extract the required characters from the string. Python s = "geeksforgeeks" # Check if the string is long enough if len(s) >= 2: # Combine first two and last two characters result = s[:2] + s[-2:] else: # Return empty string for short inputs result = "" print(result) Outputgeks Explanation:Slicing s[:2] extracts the first two characters.Slicing s[-2:] extracts the last two characters.These are concatenated to form the result.If the string has fewer than two characters, it returns an empty string.Using list comprehensionThis method uses a concise, list-based approach to solve the problem. Python s = "hello" # Combine first two and last two characters using list comprehension result = "".join([s[:2], s[-2:]]) if len(s) >= 2 else "" print(result) Outputhelo Explanation:join()method combines the sliced parts of the string.A conditional expression ensures that for strings shorter than two characters, an empty string is returned.Using regular expressionsThis method uses Python's re module for pattern matching. While not necessary for simple cases, regular expressions are useful for complex scenarios. Python import re # Input string s = "geeks" # Use regex to extract first and last two characters result = s[:2] + s[-2:] if len(s) >= 2 else "" print(result) Outputgeks Explanation:While regular expressions are not necessary for this problem, they can handle more complex requirements if needed.For this problem, s[:2] + s[-2:] is directly applied to achieve the result efficiently.Using conditional logicThis method implements inline conditional logic for compactness. Python # Input string s = "world" # Use a conditional expression to generate the result result = s[:2] + s[-2:] if len(s) >= 2 else "" print(result) Outputwold Explanation:The conditional expression directly checks the string length and generates the desired output.It avoids the need for additional libraries or functions, making it a simple and lightweight approach. Comment More infoAdvertise with us Next Article Python - Create a string made of the first and last two characters from a given string R rbbansal Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python program to extract characters in given range from a string list Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best", 4 min read Python program to capitalize the first and last character of each word in a string In this article, we will explore how to capitalize the first and last character of each word in a string in Python. It involves processing each word to transform its first and last letters to uppercase, while making the other characters lowercase.Using List Comprehensionlist comprehension and string 2 min read Python program to print k characters then skip k characters in a string Given a String, extract K characters alternatively. Input : test_str = 'geeksgeeksisbestforgeeks', K = 4 Output : geekksisforg Explanation : Every 4th alternate range is sliced. Input : test_str = 'geeksgeeksisbest', K = 4 Output : geekksis Explanation : Every 4th alternate range is sliced. Method # 5 min read Swap the First and the Last Character of a String We are given a string and our task is to swap its first and last character. For example, given the string: s = "hello" after swapping the first and last character, the result will be: "oellh"Using String SlicingWe can swap the first and last character of a string using slicing and the idea is to ext 3 min read Python program for removing i-th character from a string In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing.Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude t 2 min read Like