Python program to swap the first and the last character of a string
Python program for exchanging the start and the end character of a string:
In this tutorial, we will learn how to swap the first or start and the last or end character of a string in python. We will take the string as an input from the user. The program will ask the user to enter a string, swap the first and the last character and print out the new modified string.
Python string is immutable, i.e. we can’t modify a string. If we want to swap the first and last character, we can’t directly change the characters in the string. We need to create one different string by changing the characters.
How to solve it :
We will use string slicing to solve this problem. String slicing is the way to get all characters of a string in a range. The string is a sequence of characters and each character has its own index. The index starts from ‘0’ to the ‘length of the string – 1‘. For example, for the string ‘world’, the index of ‘w’ is ‘0‘,index of ‘o‘ is ‘1‘ etc.
We can slice a string by specifying the index numbers separated by a colon like ‘[x:y]’.
For example, for a string ‘str’,’str[2:4]” will slice the string from index ‘2‘ to index ‘3‘. Note that the second value is not the last index. If we pass ‘0‘ as the first value and length of the string as its second value, it will return us the same string.
str = "world" print(str[0:3]) print(str[1:4])
It will print :
In the above example, the first print statement is printing the sub-string from the index ‘0‘ to ‘2‘. The second print statement is printing from the index ‘1‘ to ‘3‘.
The first index and the last index values are optional. If we don’t provide the first index, it will consider it from the index ‘0‘ and if we don’t provide the second index, it will take all the characters to the end starting from the start index.
str = "world" print(str[:3]) print(str[1:])
In this example, the first index is empty for the first print statement and the second index is empty for the second print statement.
We can also use a negative index with a string in python. The last character can be accessed with index ‘-1’, the second last with ‘-2‘ etc. For example, for the string ‘world‘, ‘d‘ has index -1,’l’ has index -2,’r’ has index -3, etc.
The negative index can also be used to slice a string. For swapping the first and the last character of a string, we will use slicing with a negative index.
Python program :
str = input("Enter a string : ") new_str = str[-1:] + str[1:-1] + str[:1] print(new_str)
Sample Output :
Enter a string : python nythop Enter a string : sample eampls Enter a string : one line ene lino Enter a string : hello world dello worlh
The above example program is used to swap the first and the last character of the string ‘str’. We have used the below line to swap the characters in the string :
str[-1:] + str[1:-1] + str[:1]
Let’s break it down :
str[-1:]: It will return all the characters from the index -1 to the last index. Since -1 is the last index of a string, it will actually return the last character of the given string str.
str[1:-1]: It will return all the characters starting from the index 1 to the character before the character with index -1.i.e. all the characters from index 1 to the last second character.
str[:1]: It will return the characters starting from the start index to the character before index 1, i.e. it will return the first character.
As you can see, concatenating all the above characters give us the required string. For string hello,
str[-1:] = 'o' str[1:-1] = 'ell' str[:1] = 'h'
String slicing is a great way to pick any part of a string in python. Slicing is available in many other programming languages. It saves a lot of time and effort for us. Negative indexing is one more handy thing in a python string. Try to run the program using python3 and drop one comment below if you have any queries.
You might also like :
- Find out the multiplication of two numbers in Python
- Write a python program to reverse a number
- Python tutorial to calculate the sum of two string numbers
- Python program to check if a number is abundant/excessive or not
- Python program to find the first odd Abundant or excessive number
- Python program to find the square root of a number