Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Home Forums Cody Bank Check Special Character in a Text using Python 3

Viewing 0 reply threads
  • Author
    Posts
    • #265
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      Given a string S, check whether it contains any special character or not. Print ‘YES’ if it does else ‘NO’.

      Input Format:

      The first line contains the string S

      Output Format:

      Print ‘YES’ or ‘NO’

      Example:

      Input:
      Hi$my*name

      Output:
      YES

      Sample Test Cases
      Test Case 1 123romeo hi NO
      Test Case 2 This is an alpha23numeric No
      Test Case 3 @#$% YES
      Test Case 4 my%name*is@amit YES
      Test Case 5 my name is Amit Verma NO
      Test Case 6 Simran*loves$python YES

      ###Code start Here###

      “””

      @author: Abhishek
      “””

      import re

      # Function checks if the string
      # contains any special character
      def run(string):

      # Make own character set and pass
      # this as argument in compile method
      regex = re.compile(‘[@_!#$%^&*()<>?/\|}{~:]’)

      # Pass the string in search
      # method of regex object.
      if(regex.search(string) == None):
      print(“NO”)

      else:
      print(“YES”)

      S = input()

      run(S)

Viewing 0 reply threads
  • You must be logged in to reply to this topic.