Important Python program swap string first part second part CBSE class 11

Python program swap string first part second part CBSE class 11In this article I am going to discuss Important Python program swap string first part second part CBSE class 11. Let us start!

Python program swap string first part second part CBSE class 11 – Program Definition

Write a Python program to swap the first part and the second part of the string Word. Assuming there are an even number of letters in the string Word. The program should finally display the changed Word.

For example :
If Word = ‘Elephant’ then the function should convert Word to ‘hantElep’ and display the output as:
Changed Word is hantElep

Steps of Python program swap string first part second part CBSE class 11 

To do this program follow these steps:

  1. Input the string
  2. Find the length of the given string using len()
  3. Declare a variable for half string and floor division by 2
  4. Take a new variable to store new string
  5. Use the if condition to check even length
  6. Initialize new word with slicing of half string
  7. Print the output

Code Python program swap string first part second part CBSE class 11 

s=input("Enter the string:")
l=len(s)
h=l//2
nw=' '

if l%2==0:
  nw=s[h:]+s[:h]
else:
  print("String does not contain even number length")
print(nw)

Folllow this link for more programs:

Computer Science Class 11 Practical Program

Download the python code:

Download Python program

Leave a Reply