Python Programming Questions - STRING

Python Programming Questions - Operation on STRING[SET-3]

1.Python Program to check if a string contains any special character

import re

def SpecialCharacterCheck(inputString):
    regex = re.compile(('[@_!#$%^&*()<>?/\|}{~:]'))
    if(regex.search(inputString) == None):
        print(inputString," Does Not HAve Any Special Character.")
    else:
        print(inputString," Does Has Special Character.")

inputString = input(" Enter The String : ")
SpecialCharacterCheck(inputString)


  Enter The String : Python$Regula&
  Python$Regula&  Does Has Special Character.



2.Python Program for Generating random strings until a given string is generated

import string
import random
import time

inputString = input(" Enter The String : ")

my_possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' ., !?;:'
my_attemptThis = ''.join(random.choice(my_possibleCharacters)
   for i in range(len(inputString)))
my_attemptNext = ''
com = False
iteration = 0

while com == False:
   print(my_attemptThis)
   my_attemptNext = ''
   com = True
   for i in range(len(inputString)):
      if my_attemptThis[i] != inputString[i]:
         com = False
         my_attemptNext = my_attemptNext + random.choice(my_possibleCharacters)
      else:
         my_attemptNext = my_attemptNext + inputString[i] 
   iteration = iteration + 1
   my_attemptThis = my_attemptNext
   time.sleep(0.1)

print("String matched after " + str(iteration) + " iterations")


 Enter The String : Python
 KSYZD1
 53HHm
 N:Q6HY
 Q1!IMM
 j3nd7Y
 ud5l 5
 L?ismT
 MoMx;w
 xwu.K5
 oRimj9
 Fy ,E.
 ..
 ..
 ..
 ..
 ..
 Python
 String matched after 103 iterations



3.Python Program to Find words which are greater than given length

inputString = input(" Enter The String : ")
word = inputString.split(' ')
requiredList = []
k  = int(input(" Enter The Minimum Length Required "))
for i in word:
    if len(i) > k:
        requiredList.append(i)

out = str(requiredList)
print(out)


  Enter The String : Computer Engineering Is Really fun With Python
  Enter The Minimum Length Required 5
  ['Computer', 'Engineering', 'Really', 'Python']



4. Python program for removing i-th character from a string

 inputString = input(" Enter The String : ")
 ithValue = int(input(" Enter The Index Value : "))
 newString = ''
 newString = newString + inputString.replace(inputString[ithValue],'',1)
 print(" String Before Removing : ",inputString)
 print(" String After Removing : ",newString)


  Enter The String : Computer
  Enter The Index Value : 7
  String Before Removing :  Computer
  String After Removing :  Compute



5.Python program to split and join a string

 inputString = input(" Enter The String : ")
word = inputString.split(' ')

#WithoutDelimeter
print(word)

#WithDelimiter
delimiter = input(" Enter The Delimiter You Want To Insert : ")
out = delimiter.join(word)
print(out)


  Enter The String : Python Is Easiest Programming Language.
  ['Python', 'Is', 'Easiest', 'Programming', 'Language.']
  Enter The Delimiter You Want To Insert : $$
  Python$$Is$$Easiest$$Programming$$Language.


  Enter The String : Python Is Easiest Programming Language.
  ['Python', 'Is', 'Easiest', 'Programming', 'Language.']
  Enter The Delimiter You Want To Insert :   $
  Python  $  Is  $  Easiest  $  Programming  $  Language.

 

Comments

  1. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete

Post a Comment