How can i judge 1st string position is correct number - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can i judge 1st string position is correct number (/thread-22086.html) |
How can i judge 1st string position is correct number - christing - Oct-29-2019 May i ask. what function i can use to make me confirm my string position number one number if i get 1, i will get ok else i will get fail. below is the function i using on my program mystring = "11000000000000000000000000000000" mystring[0:1] if mystring == 1: print(ok) else: print(fail) RE: How can i judge 1st string position is correct number - perfringo - Oct-29-2019 There is str.startswith() method which: So:>>> my_string = '1100' >>> my_string.startswith('1') True >>> my_string.startswith('0') FalseYou can see all the str methods by writing str. into interactive interpreter and pressing TAB two times:>>> str. str.capitalize( str.index( str.isspace( str.replace( str.strip( str.casefold( str.isalnum( str.istitle( str.rfind( str.swapcase( str.center( str.isalpha( str.isupper( str.rindex( str.title( str.count( str.isascii( str.join( str.rjust( str.translate( str.encode( str.isdecimal( str.ljust( str.rpartition( str.upper( str.endswith( str.isdigit( str.lower( str.rsplit( str.zfill( str.expandtabs( str.isidentifier( str.lstrip( str.rstrip( str.find( str.islower( str.maketrans( str.split( str.format( str.isnumeric( str.mro( str.splitlines( str.format_map( str.isprintable( str.partition( str.startswith(You can find what any of these methods do by using built-in help: >>> help(str.capitalize) Help on method_descriptor: capitalize(self, /) Return a capitalized version of the string. More specifically, make the first character have upper case and the rest lower case.Press Q to exit help. RE: How can i judge 1st string position is correct number - christing - Oct-30-2019 GOOD DAY, THANKS FOR YOU REPLY MY MESSAGE. After i try your method i am still no success TO DO IT IS I MISSING ANYTHING. RE: How can i judge 1st string position is correct number - newbieAuggie2019 - Oct-30-2019 (Oct-29-2019, 04:00 AM)christing Wrote: May i ask. what function i can use to make me confirm my string position number one number if i get 1, i will get ok else i will get fail. Hi! Maybe this can give you some ideas: mystring = "11000000000000000000000000000000" for character in mystring: if character == '1': print(f"O.K., the character '{character}' in my string is '1'.") else: print(f"Fail, the character '{character}' in my string is not '1'.")and that produces the following output: I hope it helps.All the best, Hi again! If you are looking for a certain position in the string, this may give you some ideas (although it's not very good code, but I think it's easier for newbies): mystring = "abcdefghijk" for positions in range(len(mystring)): if mystring[positions] == 'g': print(f"O.K., the character '{mystring[positions]}' in my string is 'g'.") else: print(f"Fail, the character '{mystring[positions]}' in my string is not 'g'.")that produces the following output: So you can check a character in a certain position, for instance: I hope it helps.All the best, |