- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Form a New String where the First Character and the Last Character have been Exchanged
When it is required to form a new string where the first and last characters are exchanged, a method can be defined that uses indexing to form the new string.
Below is the demonstration of the same −
def exchange_val(my_string): return my_string[-1:] + my_string[1:-1] + my_string[:1] my_string = “Hi there how are you” print(“The string is :”) print(my_string) print(“The modified string is :”) print(exchange_val(my_string))
The string is : Hi there how are you The modified string is : ui there how are yoH
A method named ‘exchange_val’ is defined that takes a string as a parameter.
It uses indexing to exchange the first and last characters of a string.
Outside the method, a string is defined, and is displayed on the console.
This method is called by passing the string as a parameter to it.
This is displayed as output on the console.
- How to remove the first and last character in a string in R?
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- First Unique Character in a String in Python
- Program to find the index of first Recurring Character in the given string in Python
- Java Program to Capitalize the first character of each word in a String
- How to remove the last character from a string in Java?
- How to find the first character of a string in C#?
- Find the first repeated character in a string using C++.
- How to cut only the first character in MySQL string?
- How to remove the first character of string in PHP?
- Finding the last occurrence of a character in a String in Java
- Java Program to replace all occurrences of a given character with new character
- Find the last non repeating character in string in C++
- Select all except the first character in a string in MySQL?
- Remove all except the first character of a string in MySQL?