String Functions in Python - II | count, find, index, isalpha, isdigit and isalnum

 


Python String count() Method

Definition and Usage

The count() method returns the number of times a specified value appears in the string.

Syntax

string.count(value, start, end)

Parameter Values

Parameter

Description

value

Required. A String. The string to value to search for

start

Optional. An Integer. The position to start the search. Default is 0

end

Optional. An Integer. The position to end the search. Default is the end of the string

Example

Return the number of times the value "e" appears in the string:

a = "Hello, and welcome to my Channel"
x = a.count("e"
)
print
(x)

or

x = "Hello, and welcome to my Channel"
print(x.count(‘e’))

Output      :     4

Example

Search from position 3:

a = "Hello, and welcome to my Channel"
x = a.count("e"
3)
print
(x)

or

x = "Hello, and welcome to my Channel"
print(x.count(‘e’,3))

Python String find() Method

Definition and Usage

The find() method finds the first occurrence of the specified value.

The find() method returns -1 if the value is not found.

The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found. (See example below)

Syntax

string.find(value, start, end)

Parameter Values

Parameter

Description

value

Required. The value to search for

start

Optional. Where to start the search. Default is 0

end

Optional. Where to end the search. Default is to the end of the string

Example

Where in the text is the word "welcome"?:

txt = "Hello, welcome to my Channel."
x = txt.find("welcome"
)
print
(x)

Example

Where in the text is the first occurrence of the letter "e"?:

txt = "Hello, welcome to my Channel."
x = txt.find("e"
)
print
(x)

Example

Where in the text is the first occurrence of the letter "e" when you only search between position 5 and 10?:

txt = "Hello, welcome to my Channel."
x = txt.find("e"
510)
print
(x)

Example

If the value is not found, the find() method returns -1, but the index() method will raise an exception:

txt = "Hello, welcome to my world."
print
(txt.find("x"))
print(txt.index("x"))

Python String index() Method

Definition and Usage

The index() method finds the first occurrence of the specified value.

The index() method raises an exception if the value is not found.

The index() method is almost the same as the find() method, the only difference is that the find() method returns -1 if the value is not found. (See example below)

Syntax

string.index(value, start, end)

Parameter Values

Parameter

Description

value

Required. The value to search for

start

Optional. Where to start the search. Default is 0

end

Optional. Where to end the search. Default is to the end of the string

Example

Where in the text is the word "welcome"?:

y = "Hello, welcome to my Channel."
x = y.index("welcome"
)
print
(x)

Example

Where in the text is the first occurrence of the letter "e"?:

a = "Hello, welcome to my Channel."
x = a.index("e"
)
print
(x)

Example

Where in the text is the first occurrence of the letter "e" when you only search between position 5 and 10?:

a = "Hello, welcome to my channel."
x = a.index("e"
510)
print
(x)

Example

If the value is not found, the find() method returns -1, but the index() method will raise an exception:

a = "Hello, welcome to my channel."
print
(a.find("z"))
print(a.index("z"))

Python String isalpha() Method

Definition and Usage

The isalpha() method returns True if all the characters are alphabet letters (a-z) otherwise returns False.

Example of characters that are not alphabet letters: (space)!#%&? etc.

Syntax

string.isalpha()

Parameter Values

No parameters.

Example

Check if all the characters in the text are letters:

a = "Computer"
x = a.isalpha()
print
(x)

Or

a = "Computer"
print(a.isalpha())

Output      :     True

Example

Check if all the characters in the text is alphabetic:

a = "Computer10"
x = a.isalpha()
print
(x)

Or

a = "Computer10"
print(a.isalpha())

Output      :     False

Python String isdigit() Method

Definition and Usage

The isdigit() method returns True if all the characters are digits, otherwise False.

Exponents, like ², are also considered to be a digit.

Syntax

string.isdigit()

Parameter Values

No parameters.

Example

Check if all the characters in the text are digits:

a = "12345"
x = a.isdigit()
print
(x)

Or

a = "12345"
print(a.isdidit())

Output      :     Ture

Example

Check if all the characters in the text are digits:

a = "\u0030" #unicode for 0
b = "\u00B2"
 #unicode for ²
print
(a.isdigit())
print(b.isdigit())

Output      :     Ture

Output      :     Ture

Example

Check if all the characters in the text are digits:

a = "12345ab"
x = a.isdigit()
print
(x)

Or

a = "12345ab"
print(a.isdidit())

Output      :     False

Python String isalnum() Method

Definition and Usage

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9) otherwise False.

Example of characters that are not alphanumeric: (space)!#%&? etc.

Syntax

string.isalnum()

Parameter Values

No parameters.

Example

Check if all the characters in the text are alphanumeric:

a = "Company12"
x = a.isalnum()
print
(x)

Or

a = "Company12"
print(a.isalnum())

Output      :     True

Example

Check if all the characters in the text is alphanumeric:

a = "Company 12"
x = a.isalnum()
print
(x)

Or

a = "Company 12"
print(a.isalnum())

Output      :     False

Method

Description

capitalize()

Converts the first character to upper case

casefold()

Converts string into lower case

center()

Returns a centered string

count()

Returns the number of times a specified value occurs in a string

encode()

Returns an encoded version of the string

endswith()

Returns true if the string ends with the specified value

expandtabs()

Sets the tab size of the string

find()

Searches the string for a specified value and returns the position of where it was found

format()

Formats specified values in a string

format_map()

Formats specified values in a string

index()

Searches the string for a specified value and returns the position of where it was found

isalnum()

Returns True if all characters in the string are alphanumeric

isalpha()

Returns True if all characters in the string are in the alphabet

isascii()

Returns True if all characters in the string are ascii characters

isdecimal()

Returns True if all characters in the string are decimals

isdigit()

Returns True if all characters in the string are digits

isidentifier()

Returns True if the string is an identifier

islower()

Returns True if all characters in the string are lower case

isnumeric()

Returns True if all characters in the string are numeric

isprintable()

Returns True if all characters in the string are printable

isspace()

Returns True if all characters in the string are whitespaces

istitle()

Returns True if the string follows the rules of a title

isupper()

Returns True if all characters in the string are upper case

join()

Converts the elements of an iterable into a string

ljust()

Returns a left justified version of the string

lower()

Converts a string into lower case

lstrip()

Returns a left trim version of the string

maketrans()

Returns a translation table to be used in translations

partition()

Returns a tuple where the string is parted into three parts

replace()

Returns a string where a specified value is replaced with a specified value

rfind()

Searches the string for a specified value and returns the last position of where it was found

rindex()

Searches the string for a specified value and returns the last position of where it was found

rjust()

Returns a right justified version of the string

rpartition()

Returns a tuple where the string is parted into three parts

rsplit()

Splits the string at the specified separator, and returns a list

rstrip()

Returns a right trim version of the string

split()

Splits the string at the specified separator, and returns a list

splitlines()

Splits the string at line breaks and returns a list

startswith()

Returns true if the string starts with the specified value

strip()

Returns a trimmed version of the string

swapcase()

Swaps cases, lower case becomes upper case and vice versa

title()

Converts the first character of each word to upper case

translate()

Returns a translated string

upper()

Converts a string into upper case

zfill()

Fills the string with a specified number of 0 values at the beginning

 <<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click Download VBA Code Notepad file - Click here D...

Blogger द्वारा संचालित.