Friday, December 27, 2024
Google search engine
HomeLanguagesPython Two Given Strings and Swap First Two Characters of Each String

Python Two Given Strings and Swap First Two Characters of Each String

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

  1. Python Program to Convert Uppercase to Lowercase
  2. Convert String Lowercase to Uppercase in Python
  3. Python First Character of String Uppercase
  4. Python Concatenate String and Variable (int, float, etc)
  5. How to Find Length of String in Python
  6. Python: String Join Function
  7. Python String Append Examples
  8. How to replace a character in a string in python
  9. Python – Count Number of Occurrences in String
  10. Python Remove Last Character from String
  11. Python Program to Reverse String
  12. Python Program to Remove First Occurrence of Character in a String
  13. Python: Remove Special Characters from String
  14. Python Program Count vowels in String
  15. Python Split a String into Array of Characters, Space
  16. Python Program String Find
  17. Python: Two Given Strings and Swap First Two Characters of Each String
  18. Print All Permutations of Given String in Python
  19. Python | Check Whether a String Contains Number or Letter
  20. Python: Convert String to Float & Float to String
  21. Python Program to Reverse String Using Stack
  22. How To Convert Python Int to String and String to Int
  23. Python Convert String to List and List to String
RELATED ARTICLES

Most Popular

Recent Comments