payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Similar Methods   in Category: Python   by amit

🕙 Posted on 2023-07-15 at 10:17:53     Read in Hindi ...


len() vs. count(), index(), rindex()

    In previous page, you have seen that len() is a built-in function, which outputs total number of characters in string literal or total number of items (elements) in list, tuple, etc. It returns the number of items in a container. Error will be shown when len() is applied on integers, floats, or boolean literals.

print( len( 12 ) )   # Outputs TypeError as shown below:

Traceback (most recent call last):
 File "C:\xampp\htdocs\python2023\new.py", line 1, in <module>
   print( len( 12 ) )
TypeError: object of type 'int' has no len()

print( len( 12.5 ) )   # Outputs TypeError

Traceback (most recent call last):
 File "C:\xampp\htdocs\python2023\new.py", line 1, in <module>
   print( len( 12.5 ) )
TypeError: object of type 'float' has no len()

print( len( True ) )   # Outputs TypeError

Traceback (most recent call last):
 File "C:\xampp\htdocs\python2023\new.py", line 1, in <module>
   print( len( True ) )
TypeError: object of type 'bool' has no len()

print( len( '12' ) )   # Outputs 2 because there are two characters in string literal

print( len( [ 12 ] ) )   # Outputs 1 because there is only one item in the list

print( len( ( 12, ) ) )   # Outputs 1 because there is only one item in the tuple

    To write a single item in a tuple, you have to place , (comma) separator inside the parentheses, just after the value, otherwise it will be interpreted as integer, float, string, boolean data-type. You will learn more about list and tuple data-types in next pages.

count() method

    There is also a method namely, count() available for some data-types, that is, string, list, tuple, bytearray and bytes. A list all methods's name available for each data-type is shown in example page for comparable study. You can see in following description that functionality of count() method is similar for these data-types mentioned herein. Other data-types don't have the count() method.

print( help( list.count ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

count(self, value, /)
    Return number of occurrences of value.

None

print( help( tuple.count ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

count(self, value, /)
    Return number of occurrences of value.

None

print( help( str.count ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

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

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end]. Optional arguments start and end are
    interpreted as in slice notation.

None

print( help( bytearray.count ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

count(...)
    B.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of subsection sub in
    bytes B[start:end]. Optional arguments start and end are interpreted
    as in slice notation.

None

print( help( bytes.count ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

count(...)
    B.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of subsection sub in
    bytes B[start:end]. Optional arguments start and end are interpreted
    as in slice notation.

None

print( len( 'Hello World' ) )   # Outputs 11

print( 'Hello World'.count( 'l' ) )   # Outputs 3

print( 'Hello World'.count( 'rl', 5, 10 ) )   # Outputs 1

    In the above example, the count() method takes three arguments, the first argument is the sub-string to be found. The optional second and third arguments are starting position (included), and ending position (excluded) respectively. Position is the index number of parent string in which sub-string is to be searched. The output is the number of occurr­ences of sub-string, when the count() method is applied on a string data-type literal.

    When the count() method is applied on a bytearray or a bytes data-type literal, the process of placing arguments are similar to string data-type. You should be careful, when writing the third argument, which is the index number of parent data-type literal, and excluded in all three situations. If you write 9 as third argument in above example, the output will be 0 (zero) because the count() method doesn't find the sub-string 'rl'.

print( 'Hello World'.count( 'r', 5, 9 ) )   # Outputs 1

print( [ 1, 2, 3, 2, 1 ].count( 2 ) )   # Outputs 2

print( ( 1, 2, 3, 2, 1 ).count( 3 ) )   # Outputs 1

    In above two examples, only one argument ( value of an item/element) must be given in case of list and tuple data-types.

index() method

    The index() method is available for string, list, tuple, bytearray and bytes data-types. The list and tuple data-types can contain 9223372036854775807 items/elements, and therefore, you have to place three arguments for them (second and third arguments are optional). For string, bytearray and bytes data-types also, the second and third arguments are optional. In all these data-types, the index() method will throw ValueError when the value of item/element, or sub-string is not found.

print( help( list.index ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

index(self, value, start=0, stop=9223372036854775807, /)
    Return first index of value.

    Raises ValueError if the value is not present.

None

print( help( tuple.index ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

index(self, value, start=0, stop=9223372036854775807, /)
    Return first index of value.

    Raises ValueError if the value is not present.

None

print( help( str.index ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

index(...)
    S.index(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.

    Raises ValueError when the substring is not found.

None

print( help( bytearray.index ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

index(...)
    B.index(sub[, start[, end]]) -> int

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

    Raises ValueError when the subsection is not found.

None

print( help( bytes.index ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

index(...)
    B.index(sub[, start[, end]]) -> int

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

    Raises ValueError when the subsection is not found.

None

print( 'Hello World'.index( 'l' ) )   # Outputs 2 (index number of first character 'l')

print( 'Hello World'.index( 'l', 5, 10 ) )   # Outputs 9 (index number of character between 5 (inclusive) and 10 (exclusive) )

print( 'Hello World'.index( 'r', 5, 9 ) )   # Outputs 8 (The second and third argument are optional when there is only one 'r' in parent string.)

print( [ 1, 2, 3, 2, 1 ].index( 2 ) )   # Outputs 1

print( [ 1, 2, 3, 2, 1 ].index( 2, 2, 4 ) )   # Outputs 3

print( ( 1, 2, 3, 2, 1 ).index( 3 ) )   # Outputs 2

    You can see index number of each character in 'Hello World' in the table mentioned in previous page. The index number of items in a list or a tuple, starts also with 0 (zero).

rindex() method

    The rindex() method is available only for string, bytearray and bytes data-types.

print( help( str.rindex ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

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

    Return the highest 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.

    Raises ValueError when the substring is not found.

None

print( help( bytearray.rindex ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

rindex(...)
    B.rindex(sub[, start[, end]]) -> int

    Return the highest index in B where subsection sub is found,
    such that sub is contained within B[start,end].  Optional
    arguments start and end are interpreted as in slice notation.

    Raise ValueError when the subsection is not found.

None

print( help( bytes.rindex ) )

C:\xampp\htdocs\python2023>py new.py
Help on method_descriptor:

rindex(...)
    B.rindex(sub[, start[, end]]) -> int

    Return the highest index in B where subsection sub is found,
    such that sub is contained within B[start,end].  Optional
    arguments start and end are interpreted as in slice notation.

    Raise ValueError when the subsection is not found.

None

print( 'Hello World'.rindex( 'l' ) )   # Outputs 9 (index number of last character 'l')

print( 'Hello World'.rindex( 'l', 0, 5 ) )   # Outputs 3 (index number of last character between 0 (inclusive) and 5 (exclusive) )

print( 'Hello World'.rindex( 'r', 5, 9 ) )   # Outputs 8 (The second and third argument are optional when there is only one 'r' in parent string.)

    In the second example, 'Hello World'.rindex( 'l', 0, 3 ) will output 2, as the third argument is exclusive, and therefore, 'l' at index number 3, will not be displayed. In these three data-types, that is, string, bytearray and bytes, the rindex() method will throw ValueError when the value of item/element, or sub-string is not found.


Leave a Comment: