Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 find a character in a string in python3
Hello, how do I find a specific character in a string? for example, str="Hello world"
I need it to ba able to return the character "w", not just its index. Thanks.
What have you tried? How is it not working?
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

I am just wondering if there are some kinds of string functions in python like in php or javascript
The 'in' operator will tell you if one string exists within another one. The index and find methods of strings will tell you where in a string another string is. Regular expressions are supported through the re module.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Thanks for the reply.
when I use, it returns something like this: <_sre.SRE_Match object; span=(6, 7), match='w'>.

Basically, I have serveral sensors output data throurgh serial to the pi. pi receives a serial stream string wt 30 C, now in my python script, I need it to be able to extract the device name and the value such as:


How is this done in python? Thanks.
I guess that it's one string object like a log file?
# simulate a file
>>> data = '''\
... device=wt
... value=30'''

>>> d = {}
>>> for line in data.split('\n'):
...     device, value = line.split('=')
...     d[device] = value

# So it's now a dictionary
 >>> d
{'device': 'wt', 'value': '30'}
>>> d['value']
>>> d['device']
(Sep-26-2017, 11:44 PM)tony1812 Wrote: I am just wondering if there are some kinds of string functions in python like in php or javascript

Yes.  Almost everything is an object in python, and any object is something you can inspect to get info about what's available to do with it.  Using strings as an example, you can see all the methods available using dir()
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
If you're not sure what one of those is, or if it takes arguments, the help() function can... help.
>>> help(str.find)
Help on method_descriptor:

    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

You can also help() the entire object, instead of one of it's methods/properties (although that's normally less helpful than just looking up the docs):
>>> help(str)
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  Methods defined here:
 |  __add__(self, value, /)
 |      Return self+value.
 |  __contains__(self, key, /)
 |      Return key in self.
 |  __eq__(self, value, /)
 |      Return self==value.
 |  __format__(...)
 |      S.__format__(format_spec) -> str
 |      Return a formatted version of S as described by format_spec.
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
-- More  --
buran likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 130 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  How to get the index of a character from a string chihaya 1 130 Dec-03-2019, 12:54 PM
Last Post: buran
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 237 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  python gives wrong string length and wrong character thienson30 2 223 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  How to Find & Count String Patterns Between two Markers in a HTML file ahmedwaqas92 3 304 Aug-19-2019, 10:12 AM
Last Post: ahmedwaqas92
  python3 List to array or string to extract data batchenr 4 543 May-28-2019, 01:44 PM
Last Post: buran
  Find string and add character - newbi PyDK 1 267 May-15-2019, 01:22 PM
Last Post: ichabod801
  How can I find a string in sequence (with Booleans)? go127a 3 361 Apr-23-2019, 01:58 PM
Last Post: ichabod801
  remove string character from url jacklee26 10 1,165 Mar-25-2019, 03:56 PM
Last Post: Larz60+
  Replace changing string including uppercase character with lowercase character silfer 11 971 Mar-25-2019, 12:54 PM
Last Post: silfer

Forum Jump:

Users browsing this thread: 1 Guest(s)