{"id":10203,"date":"2021-09-30T12:00:57","date_gmt":"2021-09-30T06:30:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=10203"},"modified":"2021-11-22T18:34:39","modified_gmt":"2021-11-22T13:04:39","slug":"python-program-to-form-a-new-string-where-the-first-character-and-the-last-character-have-been-exchanged","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-form-a-new-string-where-the-first-character-and-the-last-character-have-been-exchanged\/","title":{"rendered":"Python Program to Form a New String where the First Character and the Last Character have been Exchanged"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing something like this.<\/p>\n Strings are considered immutable in Python, once created, they cannot be modified. You may, however, construct new strings from existing strings using a variety of approaches. This form of programming effort is known as string manipulation.<\/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 are the ways to form a new string in which the first and last characters have been swapped<\/p>\n Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/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 Related Programs<\/strong>:<\/p>\n Strings in Python: A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing …<\/p>\ngiven_string='btechgeeks'<\/code><\/p>\n
given string = hello this is btechgeeks<\/pre>\n
The original string before modification = hello this is btechgeeks\r\nThe new string after modification = sello this is btechgeekh<\/pre>\n
given string = this is btechgeeks online coding platform for geeks<\/pre>\n
Enter some random string = this is btechgeeks online coding platform for geeks\r\nThe original string before modification = this is btechgeeks online coding platform for geeks\r\nThe new string after modification = shis is btechgeeks online coding platform for geekt<\/pre>\n
Program to Form a New String where the First Character and the Last Character have been Exchanged<\/h2>\n
\n
1)Using slicing(static input)<\/h3>\n
\n
# Take a function that swaps the first and last character of the string.\r\ndef swapString(given_string):\r\n # Split the string in the function.\r\n # The last character is then added to the middle half of the string,\r\n # which is then added to the first character.\r\n modifiedstring = given_string[-1:] + given_string[1:-1] + given_string[:1]\r\n # The modified string will be returned.\r\n return modifiedstring\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngiven_string = 'hello this is btechgeeks'\r\n# printing the original string before modification\r\nprint('The original string before modification =', given_string)\r\n# Pass the given string as an argument to the function.\r\nmodified_string = swapString(given_string)\r\n# printing the new string after modification\r\nprint('The new string after modification =', modified_string)\r\n<\/pre>\n
The original string before modification = hello this is btechgeeks\r\nThe new string after modification = sello this is btechgeekh<\/pre>\n
\n
2)Using slicing(User input)<\/h3>\n
\n
# Take a function that swaps the first and last character of the string.\r\ndef swapString(given_string):\r\n # Split the string in the function.\r\n # The last character is then added to the middle half of the string,\r\n # which is then added to the first character.\r\n modifiedstring = given_string[-1:] + given_string[1:-1] + given_string[:1]\r\n # The modified string will be returned.\r\n return modifiedstring\r\n\r\n\r\n# Give the string as user input using int(input()) and store it in a variable.\r\ngiven_string = input('Enter some random string = ')\r\n# printing the original string before modification\r\nprint('The original string before modification =', given_string)\r\n# Pass the given string as an argument to the function.\r\nmodified_string = swapString(given_string)\r\n# printing the new string after modification\r\nprint('The new string after modification =', modified_string)\r\n<\/pre>\n
Enter some random string = this is btechgeeks online coding platform for geeks\r\nThe original string before modification = this is btechgeeks online coding platform for geeks\r\nThe new string after modification = shis is btechgeeks online coding platform for geekt<\/pre>\n
\n