To swap first two characters of given string in python; In this python post, we would like to share with you various python programs:- To swap first two characters of given two strings in Python.
Algorithm to Swap First Two Character of Given Two String
- First of all, the program has 2 strings attached to the user and will store them in a variable.
- After this, we will swap the first two variables of the given strings using the Python slicking method and replus () method. Also keep them in new variables.
- In the last, we will print those new variables. In which we put the swap strings.
Program 1:
- Allow user to input strings one by one, which user want to swap first two characters.
- Swap string using slicing , replace() method and store result in variables.
- Print results.
#take strings from user str1 = input("Please Enter First String : ") str2 =input("Please Enter Second String : ") x=str1[0:2] str1=str1.replace(str1[0:2],str2[0:2]) str2=str2.replace(str2[0:2],x) print("Your first string has become :- ",str1) print("Your second string has become :- ",str2)
After executing the program, the output will be:
Please Enter First String : sam Please Enter Second String : mak Your first has become :- mam Your second has become :- sak
Program 2:
- Allow user to input strings one by one, which user want to swap first two characters.
- Swap string using slicing and store result in variables.
- Print results.
#take input string from user str1 = input("Please Enter First String : ") str2 =input("Please Enter Second String : ") #swap first two characters of given string x = str2[:2] + str1[2:] y = str1[:2] + str2[2:] #print result print("Your first has become :- ",x) print("Your second has become :- ",y)
After executing the program, the output will be:
Please Enter First String : sam Please Enter Second String : mack Your first has become :- mam Your second has become :- sack
Recommended Python String Programs
- Python Program to Convert Uppercase to Lowercase
- Convert String Lowercase to Uppercase in Python
- Python First Character of String Uppercase
- Python Concatenate String and Variable (int, float, etc)
- How to Find Length of String in Python
- Python: String Join Function
- Python String Append Examples
- How to replace a character in a string in python
- Python – Count Number of Occurrences in String
- Python Remove Last Character from String
- Python Program to Reverse String
- Python Program to Remove First Occurrence of Character in a String
- Python: Remove Special Characters from String
- Python Program Count vowels in String
- Python Split a String into Array of Characters, Space
- Python Program String Find
- Python: Two Given Strings and Swap First Two Characters of Each String
- Print All Permutations of Given String in Python
- Python | Check Whether a String Contains Number or Letter
- Python: Convert String to Float & Float to String
- Python Program to Reverse String Using Stack
- How To Convert Python Int to String and String to Int
- Python Convert String to List and List to String