python logo


Category: documentation

convert to tuple python

isnumeric python

The Python function .isnumeric() (which is part of every string) returns True if string is numeric. If the string is not numeric, it will return False.
You can use this in combination with if-statements which then allow you to branch your code. This is often used for input validation.

Syntax

It does not take any parameters. The format of .isnumeric() is:

str.isnumeric()

Related Course:
Python Programming Bootcamp: Go from zero to hero

Example

The example below defines a string and then checks if the string is numeric. We do this twice, first with a string that is not numeric and then with a string that is numeric.

str = u"someone speak python here? sssss"
print( str.isnumeric() )

str = u"12345"
print( str.isnumeric() )

Result:

False
True

python isdecimal

Python’s isdecimal() String Method: A Quick Guide

The isdecimal() method in Python’s string class helps in determining whether a string contains only decimal characters. It is a handy method when working with numeric validations.

Syntax

The isdecimal() method follows a straightforward format:

str.isdecimal()

Parameters

This method does not take any parameters.

How does isdecimal() work?

The method returns True if all characters in the string are decimals (0-9) and the string is not empty, otherwise, it returns False.

Examples

To better understand the method, consider the following examples:

sample_str = u"1.234"
print(sample_str.isdecimal())

sample_str = u"12345"
print(sample_str.isdecimal())

The output for the above examples will be:

False
True

This is because the first string contains a period (.), which is not a decimal character. In contrast, the second string consists only of decimal characters.

isdigit python

The isdigit() method in Python is utilized to determine if a string consists entirely of numerical digits. If the entire string is made up of digits and is not empty, the method will return True, otherwise, it will return False. It’s a handy tool for validating user input or processing strings in various scenarios.

Syntax of isdigit()

To check if a string contains only digits using the isdigit() function in Python, use the following format:

str.isdigit()

It’s important to note that this method doesn’t take any parameters.

Example of isdigit()

Below are some illustrative examples to demonstrate the use of the isdigit() method:

str_example1 = u"string digit 1234"
print( str_example1.isdigit() )

str_example2 = u"12345"
print( str_example2.isdigit() )

Result of the above code:

False
True

For further reading on string methods in Python, you can navigate through the following:

capitalize in python

The method capitalize returns a new string with first letter capitalized.

Syntax


The format is:


str.capitalize()




Parameters

None.

Example


str = "the capitalize method returns the string with first letter capitalized."
print( str.capitalize() )

Result:


The capitalize method returns the string with first letter capitalized.

python string count

Return the number of substrings in string.

Syntax


The format is:

str.count(sub)

or
str.count(sub,start,end)


Parameters

Sub substring to count

Example

str = "The string count method returns the number of sub string occurences. "
print( str.count("string") )

Result:

2

python string index

python string length

Python’s len() function is used to determine the length of a string. This fundamental function is critical when working with strings in Python.

Syntax

To determine the length of a string in Python, use the len() function. Here’s how:

len(str)

Parameters

When using the len() function, provide the string for which you want to determine the length as the parameter:

String

Example

Let’s look at a practical example to understand how the len() function works:

str = "All work and no play makes Jack a dull boy. Jack?"
strlen = len(str)
print(strlen)

Result

After executing the above code, the output will be:

49

Navigate through the documentation:

lower in python

Understanding the lower() Method in Python
Python provides a variety of string manipulation methods, and str.lower() is one of the most commonly used functions. It is designed to convert any uppercase characters in a string to lowercase.

Syntax of the lower() Method

If you’re looking to change the characters of a string to lowercase in Python, the syntax is straightforward:

str.lower()

No Parameters Required

The beauty of the str.lower() function is its simplicity. It doesn’t require any parameters, making it easy to use.

Practical Example of Using str.lower()

Let’s take a look at a real-world example to understand how the str.lower() function works:

s = "All work and no play makes Jack a dull boy. Jack?"
s = s.lower()
print(s)

Result of the Above Code

The string, once passed through the str.lower() function, outputs:

all work and no play makes jack a dull boy. jack?

For more insights into string manipulation in Python, feel free to navigate through our documentation. [

upper python

Transforming a string to uppercase in Python is a frequently used functionality. Python offers a straightforward method for this task: str.upper(). Here’s how to use it.

Syntax

The syntax to convert a string to uppercase in Python is:

str.upper()

This method doesn’t require any parameters and will return the original string in uppercase form.

Parameters

There are no parameters required for the str.upper() method.

Example

Let’s see the str.upper() method in action:

s = "All work and no play makes Jack a dull boy. Jack?"
s = s.upper()
print(s)

Executing the above code will produce the following result:

ALL WORK AND NO PLAY MAKES JACK A DULL BOY. JACK?

The original string has been transformed to its uppercase version.

python string replace