Saturday, 31 December 2022

Python program to search a word in a given string

  Python String  


Search a Word in a given String

  • We can use "in" keyword to search a word in a given string.

Example:


myText = "Wishing You All Happy and Healthy 2023 "
searchWord = "2023"

if (searchWord in myText):
    print(searchWord, "is present in myText variable")
else:
    print(searchWord, "is not present in myText variable")

Program Output :

      2023 is present in myText variable


Recommended Articles:

 


 Home 

 Next 

Monday, 11 July 2022

Python program to reverse a given string

  Python String  

Python program to reverse a given string. 

This program will read a string from user and prints reversed string.

Program Code :

# Python program to reverse a given string.

str=input("Enter your string:")
print("The original string is : " + str)

str_rev = str[::-1]
print("The reversed string is : " + str_rev)

Program Output :

Enter your string:Python Practicals 

The original string is : Python Practicals 

The reversed string is : slacitcarP nohtyP

Recommended Articles:

 


 Home 

 Next 

 

Tuesday, 17 August 2021

Program to count total number of characters in a given String

Following Python program will count total number of characters in a given string. 

Expected Output: If user enters string = Hello Students, output should be

Total Number of Characters in a String = 14

Program Code:

# Python program to count total number of characters in a given String
 
str1 = input("Enter your string: ")
count = 0  #initialize counter to 0
 
for i in str1:
    count = count + 1
 
print("Total Number of Characters in a String = ", count)

Output of Program:

Enter your string: Hello Students Total Number of Characters in a String = 14


* * * * *

< Back to Python Loops >


Saturday, 14 August 2021

Square pattern of star (*) using for loop

Following Python program will print square pattern of star (*) using for loop. Number of lines to be printed are entered by the user. 

Expected Output: If user enters lines = 3, output should be

* * * * * * * * *

Program Code:

lines = int(input("Enter number of lines:"))

for i in range(lines):    
   for i in range(lines):        
      print('*', end = '  ')    
   print()

Output of Program:

Enter number of lines:3 * * * * * * * * *


* * * * *


< Back to Python Loops >





Monday, 9 August 2021

Even numbers between 1 to 10 using for loop

Following Python program will print even numbers between 1 to 10 using for loop. 

Expected Output: 2 4 6 8 10 


Program Code:

for i in range(2, 11, 2):
  print (i, end=" ")

Output of Program:

2 4 6 8 10


* * * * *


< Back to Python Loops >





Sunday, 8 August 2021

Odd numbers between 1 to 10 using while loop

Following Python program will print odd numbers between 1 to 10 using while loop. 

Expected Output: 1 3 5 7 9 


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 2

Output of Program:

1 3 5 7 9


* * * * *


< Back to Python Loops >

< Back to Home >



Friday, 1 January 2021

Python program to print 10 to 1 using while

 Following Python program will print 10 to 1 numbers using while loop. 

Expected Output: 10 9 8 7 6 5 4 3 2 1


Program Code:

number = 10

while number >= 1:
  print(number, end=" ")
  number = number - 1


Output of Program:

10 9 8 7 6 5 4 3 2 1


* * * * *


< Back to Python Loops >

< Back to Home >

Python program to print 1 to 10 using while loop

Following Python program will print 1 to 10 numbers using while loop. 

Expected Output: 1 2 3 4 5 6 7 8 9 10


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 1


Output of Program:

1 2 3 4 5 6 7 8 9 10


* * * * *


< Back to Python Loops >

< Back to Home >



Saturday, 26 December 2020

Python program to swap two variables

Python program to swap two variables

Python Code:

# Read inputs from the user
x = input('Enter value of x: ')
y = input('Enter value of y: ')

print('Value of x before swapping: ', x)
print('Value of y before swapping: ', y)

# using temporary variable to swap values
temp = x
x = y
y = temp

print('Value of x after swapping: ', x)
print('Value of y after swapping: ', y)

Output of program:

Enter value of x: 10 Enter value of y: 20 Value of x before swapping: 10 Value of y before swapping: 20 Value of x after swapping: 20 Value of y after swapping: 10



Friday, 25 December 2020

Check whether given character is a vowel or not

Python program to check whether given character is a vowel or not.

Python Code:

myChar = input("Enter a character: ")

if(myChar=='A' or myChar=='E' or myChar=='I' or myChar=='O' or myChar=='U' 
   or myChar=='a' or myChar =='e' or myChar=='i' or myChar=='o' or myChar=='u'):
    print("'",  myChar, "' is a Vowel.")
else:
    print("'",  myChar, "'is a Consonant.")

Output of program

Enter a character: A
' A ' is a Vowel.