Symbol-only string detection

Viewed 17611

I'm fairly new to Python (and programming in general), so I often end up facing really silly issues, such as the one below.

What I want is to repeatedly check if all the characters in a user input are symbols. When such an input is entered, I want to print that string.

Since there doesn't seem to be a way to specifically test for symbols, I decided to test for letters first, and then for numbers, and if both of them come up as negative, then it should print the text.

Here is my code:

while True:

  symbols = input("Enter symbol string:")

  if symbols == symbols.isalpha():
    print (symbols.isalpha())
  elif not a == a.isalpha():
    print (a.isalpha())
    break
3 Answers

This is how i solved it..

import re

def start():
  global count
  for letter in SYMBOLS:
      if re.search(reg,SYMBOLS):
          count=count+1#just to count how many exist
          global S#if you want to store the result
          S=letter
          
      else:
          print(letter,': is not a symbol')


count = 0
SYMBOLS= input('Enter text\n')#INPUT
reg =('[@_!#$£%^&*()<>?/\|}{~:]')#SYMBOL SEARCH
start()

Slightly updated version of Farmer Joe's code. Better processing of input included.

symbols = input("Enter any characters:")

valid_symbols = "!@#$%^&*()_-+={}[]"

if symbols == '':
    print("None of characters have been entered. Please try again")
else:
    for ch in symbols:
        if symbols.isalnum() is True:
            print('No symbols have been detected in the input')
            break
        else:
            if ch not in valid_symbols:
                print('There are symbols mixed with other characters')
                break
    else:
        print('Your input contains symbols only')
Related