Swap the First and the Last Character of a String
Last Updated :
31 Jan, 2025
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 Slicing
We can swap the first and last character of a string using slicing and the idea is to extract the middle part of the string and place the last character at the beginning and the first character at the end.
Python
s = "GeeksforGeeks"
# Swap the first and last characters using slicing
if len(s) > 1:
res = s[-1] + s[1:-1] + s[0]
else:
res = s # For strings of length 1 or less, no swap needed
print(res)
Explanation:
- s[-1] + s[1:-1] + s[0] combines the last character, middle part, and first character to swap the first and last characters.
- if len(s) > 1 ensures that the swap only happens if the string has more than one character.
- else: For strings of length 1 or less, no swap is needed, and the string remains unchanged.
Let's understand different method to swap the first and last character of string.
Using List Conversion
In this approach the string is converted into a list the first and last characters are swapped, and the list is then joined back into a string.
Python
s = "GeeksforGeeks"
# Convert string to list and swap first and last characters
a = list(s)
a[0], a[-1] = a[-1], a[0]
res = ''.join(a)
print(res)
Explanation:
- a = list(s) converts the string s to a list of characters.
- a[0], a[-1] = a[-1], a[0] swaps the first and last characters.
- ''.join(a) joins the modified list back into a string.
Using f-strings
In Python f-strings allow for concise and readable string formatting by embedding expressions inside curly braces {}.They are evaluated at runtime, providing an efficient way to manipulate and concatenate strings.
Python
s = "GeeksforGeeks"
res = f"{s[-1]}{s[1:-1]}{s[0]}"
print(res)
Explanation :
- s[-1] accesses the last character of the string s. In this case, it is "s".
- s[1:-1]extracts the middle part of the string, excluding the first and last characters. For the string "GeeksforGeeks", it results in "eeksforGeek".
- s[0] accesses the first character of the string, which is "G".
Using str.join
Using str.join we can swap the first and last characters by combining the last character, middle part and first character into a new string, offering a concise solution.
Python
s = "GeeksforGeeks"
res = ''.join((s[-1], s[1:-1], s[0]))
print(res)
Explanation:
- s[-1] accesses the last character of the string s.
- s[1:-1] extracts the middle part of the string s and excluding the first and last characters.
- s[0] accesses the first character of the string s.
- ''.join(...) combines these parts into a single string without any separator.
Similar Reads
Python3 Program for Swap characters in a String Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your ta
5 min read
Python | First character occurrence from rear String There are many ways to find out the first index of element in String as python in its language provides index() function that returns the index of first occurrence of element in String. But if one desires to get the last occurrence of element in string, usually a longer method has to be applied. Let
4 min read
Reverse Alternate Characters in a String - Python Reversing alternate characters in a string involves rearranging the characters so that every second character is reversed while maintaining the original order of other characters. For example, given the string 'abcde', reversing the alternate characters results in 'ebcda', where the first, third and
3 min read
Swap elements in String list - Python Swapping elements in a string list means we need to exchange one element with another throughout the entire string list in Python. This can be done using various methods, such as using replace(), string functions, regular expressions (Regex), etc. For example, consider the original list: ['Gfg', 'is
3 min read
Python - Move Word to Rear end Sometimes, while working with Python strings, we can have a problem in which we need to find a word and move it to the end of the string. This can have application in many domains, including day-day programming and school programming. Let's discuss certain ways in which this task can be performed. M
6 min read