How do i finda spl character in a string and return found one..?

Manise.Saragih commented: i love +0

Dani AI

Generated

A few clarifications before choosing an approach: "special character" can mean punctuation only (comma, hyphen, period), any non-alphanumeric (spaces included), or Unicode symbols (emoji, currency signs). asked for a sample — the thread shows 1002-1004,1 1444, which suggests you want punctuation and separators like -, ,, and space. and explored simple searching/splitting; pointed out a short regex-based route. Below are robust, practical options that avoid repeating the exact snippets already posted.

A simple, configurable Python 3 solution that preserves first-seen order and lets you choose whether whitespace counts as "special":

from string import punctuation

def find_special_chars(s, treat_space_as_special=False):
    def is_special(ch):
        if ch.isalnum():
            return False
        if not treat_space_as_special and ch.isspace():
            return False
        return True

    return list(dict.fromkeys([ch for ch in s if is_special(ch)]))

For Unicode-aware detection (good when input may contain non-Latin punctuation or symbols such as currency signs or emoji), use the Unicode category. Categories beginning with 'P' are punctuation and 'S' are symbols:

import unicodedata

def find_unicode_punct_and_symbols(s):
    return list(dict.fromkeys([ch for ch in s
                               if unicodedata.category(ch)[0] in ('P', 'S')]))

If you prefer a compact regex and want to exclude whitespace (so you only get punctuation/symbols), compile a pattern once and reuse it for performance:

import re
pattern = re.compile(r'[^\w\s]', flags=re.UNICODE)
matches = list(dict.fromkeys(pattern.findall(s)))

Notes and troubleshooting: decide first whether underscore should count (it is a "word" character in many patterns). For very large text, build sets or use streaming to avoid memory spikes. If you need to treat visually combined emoji as single units, consider libraries or grapheme-cluster aware solutions—simple per-codepoint checks will split multi-codepoint emojis.

Recommended Answers

All 6 Replies

Your question isn't very clear; can you provide a sample input/output for the use case in consideration?

Create a tuple of special characters you will be checking for. Create a for loop and use string.find()
Maybe you can implement it as follows:

test="How are you?!!!#"
#Say you have a list of chrs like @,#,$,!,%,^,&,* etc
spl_chr='!'#If you are looking for !
loc=test.find(spl_chr)
if loc==-1:
    print "not found"
else:
    print "Found spl caracter at "+loc

Thats all. For more special characters you can create an array of spl characters and check for each one in a loop

i did something like this

strng = '1002-1004,1 1444'

[each for each in strng.split("-")[-1] if each in [',',' ','*','"','#','-']

i don't rememeber how i did in workplace, but it was better ..

this was it
import re

[char for char in strng if re.search(r"[^a-wy-zA-WYZ0-9]",char)]

Shorter.

>>> strng = '1002-1004,1 1444'
>>> re.findall(r'\W', strng)
['-', ',', ' ']

Or the same without regex.

>>> strng = '1002-1004,1 1444'
>>> [c for c in strng if c in ['-', ',', ' ']]
['-', ',', ' ']

you dont even understand the code man...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.