Python Forum
How can i judge 1st string position is correct number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i judge 1st string position is correct number
#1
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)
Reply
#2
There is str.startswith() method which:

Output:
Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.
So:

>>> my_string = '1100'                                                                     
>>> my_string.startswith('1')                                                              
True
>>> my_string.startswith('0')                                                              
False
You 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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
(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:
Output:
O.K., the character '1' in my string is '1'. O.K., the character '1' in my string is '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. Fail, the character '0' in my string is not '1'. >>>
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:
Output:
Fail, the character 'a' in my string is not 'g'. Fail, the character 'b' in my string is not 'g'. Fail, the character 'c' in my string is not 'g'. Fail, the character 'd' in my string is not 'g'. Fail, the character 'e' in my string is not 'g'. Fail, the character 'f' in my string is not 'g'. O.K., the character 'g' in my string is 'g'. Fail, the character 'h' in my string is not 'g'. Fail, the character 'i' in my string is not 'g'. Fail, the character 'j' in my string is not 'g'. Fail, the character 'k' in my string is not 'g'. >>>
So you can check a character in a certain position, for instance:
Output:
>>> mystring = "abcdefghijk" >>> mystring[0] 'a' >>> mystring[3] 'd' >>> mystring[5] 'f' >>> mystring[8] 'i' >>> mystring[10] 'k' >>>
I hope it helps.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,105 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,447 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,364 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Printing string at specific position on terminal - not showing __Mathieu__ 1 2,331 Sep-07-2020, 10:32 AM
Last Post: Larz60+
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,104 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  Make an array of string number in a List polantas 5 3,030 May-27-2020, 07:18 AM
Last Post: buran
  line number of first and second occurance of string in a file mdalireza 1 1,798 Nov-18-2019, 09:55 AM
Last Post: perfringo
  How i can judge my code christing 16 5,729 Oct-03-2019, 02:23 PM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020