Python

Python hex() Method with Examples

In the previous article, we have discussed Python float() Method with Examples
hex() Method in Python:

The hex() function converts the given number to a hexadecimal value.

The prefix 0x is always present in the returned string.

Syntax:

hex(number)

Parameters

The hex() function only accepts one argument.

x – integer number (either an int object or a __index__() method that returns an integer)

Return Value:

The hex() function converts an integer to its corresponding hexadecimal number and returns it as a string.

The hexadecimal string returned begins with the prefix 0x, indicating that it is in hexadecimal form.

Examples:

Example1:

Input:

Given Number = 20

Output:

The given number's{ 20 } Hexadecimal Value =  0x14
The hex() function Return Type = <class 'str'>

Note:

The prefix 0x denotes that the outcome is a Hexadecimal string.

Example2:

Input:

Given Number = 4.5

Output:

The given float number's{ 4.5 } Hexadecimal Value =  0x1.2000000000000p+2

hex() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the hex() function to get the hexadecimal value of a given number.
  • Store it in another variable.
  • Print the given number’s Hexadecimal Value.
  • Print the Return Type of hex() Function using the type() method by passing the hex(given number) as an argument to it.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 20
# Pass the given number as an argument to the hex() function to get the
# hexadecimal value of a given number.
# Store it in another variable.
hexadeciml_valu = hex(gvn_numbr)
# Print the given number's Hexadecimal Value.
print("The given number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)
# Print the return Type of hex() Function using the type() method by passing
# the hex(given number) as an argument to it.
print("The hex() function Return Type =", type(hex(gvn_numbr)))

Output:

The given number's{ 20 } Hexadecimal Value =  0x14
The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the hex() function and apply float to the result to get the hexadecimal value of a given float number.
  • Store it in another variable.
  • Print the given float number’s Hexadecimal Value.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 4.5
# Pass the given number as an argument to the hex() function and apply float
# to the result to get the hexadecimal value of a given float number.
# Store it in another variable.
hexadeciml_valu = float.hex(gvn_numbr)
# Print the given float number's Hexadecimal Value.
print("The given float number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)

Output:

The given float number's{ 4.5 } Hexadecimal Value =  0x1.2000000000000p+2

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the hex() function to get the hexadecimal value of a given number.
  • Store it in another variable.
  • Print the given number’s Hexadecimal Value.
  • Print the Return Type of hex() Function using the type() method by passing the hex(given number) as an argument to it.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input("Enter some random number = "))
# Pass the given number as an argument to the hex() function to get the
# hexadecimal value of a given number.
# Store it in another variable.
hexadeciml_valu = hex(gvn_numbr)
# Print the given number's Hexadecimal Value.
print("The given number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)
# Print the return Type of hex() Function using the type() method by passing
# the hex(given number) as an argument to it.
print("The hex() function Return Type =", type(hex(gvn_numbr)))

Output:

Enter some random number = 50
The given number's{ 50 } Hexadecimal Value = 0x32
The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number

Approach:

  • Give the number as user input using the float(input()) function and store it in a variable.
  • Pass the given number as an argument to the hex() function and apply float to the result to get the hexadecimal value of a given float number.
  • Store it in another variable.
  • Print the given float number’s Hexadecimal Value.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the float(input()) function and store it in a variable.
gvn_numbr = float(input("Enter some random number = "))
# Pass the given number as an argument to the hex() function and apply float
# to the result to get the hexadecimal value of a given float number.
# Store it in another variable.
hexadeciml_valu = float.hex(gvn_numbr)
# Print the given float number's Hexadecimal Value.
print("The given float number's{", gvn_numbr,
      "} Hexadecimal Value = ", hexadeciml_valu)

Output:

Enter some random number = 7.5
The given float number's{ 7.5 } Hexadecimal Value = 0x1.e000000000000p+2

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python hex() Method with Examples Read More »

Python float() Method with Examples

In the previous article, we have discussed Python complex() Method with Examples
float() Method in Python:

The float() function returns a floating-point number from the specified value.

Syntax:

float(value)

Parameter Values:

value: A number or string that can be converted to a floating-point number.

Return Value:

The float() method yields:

  • If an argument is passed, the equivalent floating-point number is returned.
  • If no arguments are passed, the value is 0.0;
  • Otherwise, an OverflowError exception is thrown if the argument is outside the range of Python float.

Examples:

Example1:

Input:

Given Number = 14

Output:

The given number's{ 14 } Floating-point number =  14.0

Example2:

Input:

Given Number = "-25.431" (# given in string format)

Output:

The given number's{ -25.431 } Floating-point number =  -25.431

float() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 14
# Pass the given number as an argument to the float() function to get the
# the floating-point number of a given number.
# Store it in another variable.
rslt = float(gvn_numbr)
# Print the floating-point number of a given number.
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ 14 } Floating-point number =  14.0

Similarly, check it out for other values and print the result

gvn_numbr = "-25.456"
rslt = float(gvn_numbr)
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

The given number's{ -25.456 } Floating-point number =  -25.456
float() for infinity and Nan(Not a number) values
print(float("InFiNiTy"))
print(float("infinity"))
print(float("nan"))
print(float("NaN"))
print(float("inf"))
print(float("InF"))

Output:

inf
inf
nan
nan
inf
inf

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as a string as user input using the input() function and store it in a variable.
  • Pass the given number as an argument to the float() function to get the floating-point number of a given number.
  • Store it in another variable.
  • Print the floating-point number of a given number.
  • The Exit of the program.

Below is the implementation:

# Give the number as a string as user input using the input() function
# and store it in a variable.
gvn_numbr = input("Enter some random number = ")
# Pass the given number as an argument to the float() function to get the
# the floating-point number of a given number.
# Store it in another variable.
rslt = float(gvn_numbr)
# Print the floating-point number of a given number.
print("The given number's{", gvn_numbr,
      "} Floating-point number = ", rslt)

Output:

Enter some random number = -19
The given number's{ -19 } Floating-point number = -19.0

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python float() Method with Examples Read More »

Python complex() Method with Examples

In the previous article, we have discussed Python bin() Method with Examples
complex() Method in Python:

By specifying a real number and an imaginary number, the complex() function returns a complex number.

Syntax:

complex(real, imaginary)

Parameters

real: This is Required. A number that represents the complex number’s real part. 0 is the default. The real number can also be a String, such as ‘2+6j’; in this case, the second parameter should be omitted.

imaginary: This is Optional. A number that represents the complex number’s imaginary part. 0 is the default.

Return Value:

The complex() method, as the name implies, returns a complex number.

The ValueError exception is thrown if the string passed to this method is not a valid complex number.

Note: Complex() always expects a string of the form real+imaginaryj or real+imaginaryJ.

Examples:

Example1:

Input:

real number = 2
imaginary number = 4

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Example2:

Input:

real number = 3

Output:

The complex number for the given real part{ 3 }=  (3+0j)

complex() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number(real part) as static input and store it in a variable.
  • Give the other number (imaginary part) as static input and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as static input and store it in a variable.
real_numbr = 2
# Give the other number (imaginary part) as static input and store it in
# another variable.
imaginry_numbr = 4
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Similarly, check for the other values

without giving imaginary part

real_numbr = 3
rslt = complex(real_numbr)
print(
    "The complex number for the given real part{", real_numbr, "}= ", rslt)

Output:

The complex number for the given real part{ 3 }=  (3+0j)

without giving any parameters

rslt = complex()
print("The complex number without giving any parameters = ", rslt)

Output:

The complex number without giving any parameters =  0j

When a number is given as a string?

gvn_numbr = "7+8j"
rslt = complex(gvn_numbr)
print(
    "The complex number for the given number {", gvn_numbr, "} = ", rslt)

Output:

The complex number for the given number { 7+8j } =  (7+8j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number(real part) as user input using the int(input()) function and store it in a variable.
  • Give the other number (imaginary part) as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as user input using the int(input()) function and store it in a variable.
real_numbr = int(input("Enter some random number = "))
# Give the other number (imaginary part) as user input using the int(input()) function 
# and store it in another variable.
imaginry_numbr = int(input("Enter some random number = "))
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

Enter some random number = 5
Enter some random number = 6
The complex number for the given real and imaginary numbers{ 5 , 6 }= (5+6j)

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.

Python complex() Method with Examples Read More »

Python List index() Method with Examples

In the previous article, we have discussed Python List extend() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List index() Method in Python:

The index() method returns the position in the given list of the given element.

Syntax:

gvnlist.index(element, start, end)

Parameter Values: 

The list index() method can accept up to three arguments:

element – the to-be-searched element

start (optional) – Begin searching from this index

end (optional) – Search the element all the way up to this index

Return Value:

The index() method returns the index of the specified list element.
A ValueError exception is thrown if the element is not found.

Note:

Only the first occurrence of the matching element is returned by the index() method.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'python', 'programs']
Given Element = 'this'

Output:

The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
1

Example2:

Input:

Given List = ['hello', 'this', 'is', 'python', 'programs']
Given Element = 'btechgeeks'

Output:

Traceback (most recent call last):
  File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module>
    resltind = gvnlst.index(gvnele)
ValueError: 'btechgeeks' is not in list

List index() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example-1: If the element is present in the given list

Approach:

  • Give the list as the static input and store it in a variable.
  • Give the element whose index must be located in the given list and store it in another variable.
  • Pass the given element to the index function for the given list and store the result in resltind variable.
  • Print the resltind value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'this'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

The given element { this } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
1
Example-2: If the element is not present in the given list

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'btechgeeks'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/d28185532d674af8eb476a05f17862ba.py", line 8, in <module>
    resltind = gvnlst.index(gvnele)
ValueError: 'btechgeeks' is not in list
Example-3: Giving Start Index

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'python'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
# searching the element after 1 st index by passing second argument as 1
resltind = gvnlst.index(gvnele, 1)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

The given element { python } in the given list ['hello', 'this', 'is', 'python', 'programs'] is present at the index :
3
Example-4: Giving Start and End Index

Below is the implementation:

# Give the list as the static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'programs']
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = 'python'
# Pass the given element to the index function for the given list
# and store the result in resltind variable.
# searching the element after 0 st index to 2 index  by passing
# second argument as 0 and third argument as 2
resltind = gvnlst.index(gvnele, 0, 2)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Traceback (most recent call last):
  File "/home/e3f1e4ebf5b32326c6525257b0b59752.py", line 10, in <module>
    resltind = gvnlst.index(gvnele, 0, 2)
ValueError: 'python' is not in list

Explanation:

The element python is present at the third index but not between the indices, resulting in an error.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element whose index must be located in the given list as user input using the input() function and store it in another variable.
  • Pass the given element to the index function for the given list and store the result in resltind variable.
  • Print the resltind value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the element whose index must be located in the given list
# and store it in another variable.
gvnele = int(input('Enter some random element = '))
# Pass the given element to the index function for the given list
# and store the result in resltind variable.

resltind = gvnlst.index(gvnele)
# Print the resltind value.
print('The given element {', gvnele, '} in the given list',
      gvnlst, 'is present at the index :')
print(resltind)

Output:

Enter some random List Elements separated by spaces = 6 2 8 9 1 8 4
Enter some random element = 8
The given element { 8 } in the given list [6, 2, 8, 9, 1, 8, 4] is present at the index :
2

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.

Python List index() Method with Examples Read More »

Python bin() Method with Examples

In the previous article, we have discussed Python String startswith() Method with Examples
Binary Number System: The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).

bin() Method in Python:

The bin() method converts and returns a given integer’s binary equivalent string. If the parameter is not an integer, the __index__() method must be used to return an integer.

Syntax:

bin(number)

Parameters

The bin() method accepts a single parameter:

number – An integer number for which the binary equivalent must be calculated.
If the value is not an integer, the __index__() method should be used to return an integer.

Return Value: 

The binary string equivalent to the given integer is returned by the bin() method.

If an integer is not specified, a TypeError exception is thrown, indicating that the type cannot be interpreted as an integer.

Examples:

Example1:

Input:

Given Number = 2

Output:

The given number's{ 2 } binary equiavalent string =  0b10

Note:

The prefix 0b denotes that the outcome is a binary string.

Example2:

Input:

Given Number = 10

Output:

The given number's{ 10 } binary equiavalent string =  0b1010

bin() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numbr = 2
# Pass the given number as an argument to the bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

The given number's{ 2 } binary equiavalent string =  0b10

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the bin() function to get the binary equivalent string of a given number.
  • Store it in another variable.
  • Print the given number’s binary equivalent string.
  • The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input("Enter some random number = "))
# Pass the given number as an argument to the bin() function to get the
# binary equivalent string of a given number.
# Store it in another variable.
binry_str = bin(gvn_numbr)
# Print the given number's binary equivalent string.
print("The given number's{", gvn_numbr,
      "} binary equiavalent string = ", binry_str)

Output:

Enter some random number = 12
The given number's{ 12 } binary equiavalent string = 0b1100

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.

Python bin() Method with Examples Read More »

Python String startswith() Method with Examples

In the previous article, we have discussed Python String replace() Method with Examples
String startswith() Method in Python:

If the string begins with the specified value, the startswith() method returns True; otherwise, it returns False.

Syntax:

string.startswith(value, start, end)

Parameters

value: This is Required. The value used to determine whether the string starts with

start: This is optional. It is an integer. The starting point for the search.

end: This is optional. It is an integer. The ending point for the search.

Return Value:

The method startswith() returns a boolean value.

  • If the string begins with the specified value, it returns True.
  • If the string does not begin with the specified value, it returns False.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks"
Given value = "Hello"

Output:

Checking if the given string startswith{ Hello } or not =  False

Example2:

Input:

Given string = "welcome to python programs"
Given value = "t"
start position = 8
end position = 13

Output:

Checking if the given string startswith{ t } or not =  True

String startswith() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"
# Give the value as static input and store it in another variable.
gvn_valu = "hello"
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ hello } or not =  True
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs"
# Give the value as static input and store it in another variable.
gvn_valu = "t"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 8
# Give the end position as static input and store it in another variable.
gvn_endpositn = 13
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Checking if the given string startswith{ t } or not =  True

Method #2: Using Built-in Functions (User Input)

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the startswith() function for the given string to check if the given string starts with the given value or not.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the startswith() function for the
# given string to check if the given string starts with the given value or not.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = hello this is btechgeeks
Enter some Random String(value) = Hello
Checking if the given string startswith{ Hello } or not = False
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the startswith() function for the given string to check if the given string starts with the given value or not in the given start and end range.
  • Store it in another variable.
  • Print the result after checking If a given string starts with the given value or not.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the startswith()
# function for the given string to check if the given string starts with the
# given value or not in the given start and end range.
# Store it in another variable.
rslt = gvn_str.startswith(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the result after checking If a given string starts with the given
# value or not.
print(
    "Checking if the given string startswith{", gvn_valu, "} or not = ", rslt)

Output:

Enter some Random String = welcome to python programs
Enter some Random String(value) = to
Enter some random number = 8
Enter some random number = 14
Checking if the given string startswith{ to } or not = True

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String startswith() Method with Examples Read More »

Python String replace() Method with Examples

In the previous article, we have discussed Python String upper() Method with Examples
String replace() Method in Python:

The replace() method replaces a phrase specified with another phrase specified.

Syntax:

string.replace(oldvalue, newvalue, count)

Note: If nothing else is specified, all occurrences of the specified phrase will be replaced.

Parameters

oldvalue: This is Required. The string to look for.

newvalue: This is Required. The string will be used to replace the old value.

count: This is optional. A number indicating how many occurrences of the old value should be replaced. The default value is all occurrences.

Return Value:

The replace() method returns a copy of the string that replaces the old value with the new value. The original string remains unaltered.

If the old substring cannot be found, the copy of the original string is returned.

Examples:

Example1:

Input:

Given String = "hello this is hello btechgeeks hello "
Given old value = "hello"
Given new value = "hai"

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hai

Example2:

Input:

Given String = "welcome to to python to to programs"
Given old value = "to"
Given new value = "all"
Given count = 3

Output:

The given string after replacing oldvalue{ to } with new value{ all } is:
welcome all all python all to programs

String replace() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving count Value

Approach:

  • Give the string as static input and store it in a variable.
  • Give the old value as static input and store it in another variable.
  • Give the new value as static input and store it in another variable.
  • Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_fststr = "hello this is hello btechgeeks hello "
# Give the old value as static input and store it in another variable.
old_valu = "hello"
# Give the new value as static input and store it in another variable.
new_valu = "hai"
# Pass the given old value, new value as arguments to the replace() function
# for the given string to replace the old value with a new value
# in a given string.
# Store it in another variable.
rslt = gvn_fststr.replace(old_valu, new_valu)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hai
2)With giving count Value

Approach:

  • Give the string as static input and store it in a variable.
  • Give the old value as static input and store it in another variable.
  • Give the new value as static input and store it in another variable.
  • Give the count as static input and store it in another variable.
  • Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_fststr = "hello this is hello btechgeeks hello "
# Give the old value as static input and store it in another variable.
old_valu = "hello"
# Give the new value as static input and store it in another variable.
new_valu = "hai"
# Give the count as static input and store it in another variable.
gvn_cnt = 2
# Pass the given old value, new value, count as arguments to the replace()
# function for the given string to replace the old value with a new value
# in a given string. Here count is a number indicating how many occurrences
# of the old value should be replaced.
# Store it in another variable.
rslt = gvn_fststr.replace(old_valu, new_valu, gvn_cnt)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

The given string after replacing oldvalue{ hello } with new value{ hai } is:
hai this is hai btechgeeks hello

Method #2: Using Built-in Functions (User Input)

1)Without giving count Value

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the old value as user input using the input() function and store it in another variable.
  • Give the new value as user input using the input() function and store it in another variable.
  • Pass the given old value, new value as arguments to the replace() function for the given string to replace the old value with a new value in a given string.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the old value as user input using the input() function and store it in another variable.
old_valu = input("Enter some Random String(oldvalue) = ")
# Give the new value as user input using the input() function and store it in another variable.
new_valu = input("Enter some Random String(newvalue) = ")
# Pass the given old value, new value as arguments to the replace() function
# for the given string to replace the old value with a new value
# in a given string.
# Store it in another variable.
rslt = gvn_str.replace(old_valu, new_valu)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

Enter some Random String = good morning btechgeeks good good morning
Enter some Random String(oldvalue) = good
Enter some Random String(newvalue) = bad
The given string after replacing oldvalue{ good } with new value{ bad } is:
bad morning btechgeeks bad bad morning
2)With giving count Value

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the old value as user input using the input() function and store it in another variable.
  • Give the new value as user input using the input() function and store it in another variable.
  • Give the count as user input using the int(input()) function and store it in another variable.
  • Pass the given old value, new value, count as arguments to the replace() function for the given string to replace the old value with a new value in a given string. Here count is a number indicating how many occurrences of the old value should be replaced.
  • Store it in another variable.
  • Print the above result i.e, The given string after replacing the old value with the given new value.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the old value as user input using the input() function and store it in another variable.
old_valu = input("Enter some Random String(oldvalue) = ")
# Give the new value as user input using the input() function and store it in another variable.
new_valu = input("Enter some Random String(newvalue) = ")
# Give count as user input using the int(input()) function and 
# store it in another variable.
gvn_cnt = int(input("Enter some random number = "))
# Pass the given old value, new value, count as arguments to the replace()
# function for the given string to replace the old value with a new value
# in a given string. Here count is a number indicating how many occurrences
# of the old value should be replaced.
# Store it in another variable.
rslt = gvn_str.replace(old_valu, new_valu, gvn_cnt)
# Print the above result i.e, The given string after replacing the old value
# with the given new value.
print("The given string after replacing oldvalue{",
      old_valu, "} with new value{", new_valu, "} is:")
print(rslt)

Output:

Enter some Random String = welcome to to python to to programs
Enter some Random String(oldvalue) = to
Enter some Random String(newvalue) = all
Enter some random number = 3
The given string after replacing oldvalue{ to } with new value{ all } is:
welcome all all python all to programs

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String replace() Method with Examples Read More »

Python String upper() Method with Examples

In the previous article, we have discussed Python String lower() Method with Examples
String upper() Method in Python:

The upper() method returns a string in which all characters are capitalized(upper case).

Symbols and numbers are not accepted.

Syntax: 

string.upper()

Parameter Values: This method has no parameters.

Return Value:

upper() returns the uppercase string from a given string.

All lowercase characters are converted to uppercase.

If no lowercase characters are found, the original string is returned.

Examples:

Example1:

Input:

Given String = "hello THIS iS btechgeeks"

Output:

The given original string : hello THIS iS btechgeeks
The given string after applying upper() method :
HELLO THIS IS BTECHGEEKS

Example2:

Input:

Given String = "gooD mornING btechgeeks @!@#123"

Output:

The given original string : gooD mornING btechgeeks @!@#123
The given string after applying upper() method :
GOOD MORNING BTECHGEEKS @!@#123

String upper() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply the upper() method to the given string that returns a string with all characters in the upper case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the upper () method.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello THIS iS btechgeeks"
# Apply upper() method to the given string that returns a string with all
# characters in upper case.
# Store it in another variable.
rslt = gvn_str.upper()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the upper() method.
print("The given string after applying upper() method :")
print(rslt)

Output:

The given original string : hello THIS iS btechgeeks
The given string after applying upper() method :
HELLO THIS IS BTECHGEEKS

How does upper() function work in a program?

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Converting both the given first and second strings into uppercase using the upper() method and checking if both the strings are the same or not using the if conditional statement.
  • If it is true, then print “The given first and second strings are same”.
  • Else print “The given first and second strings are NOT same”.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "WELCOME to python PROGRAMS"
# Give the second string as static input and store it in a variable.
gvn_scndstr = "Welcome to PYTHON programs"
# Converting both the first and second strings into uppercase using the upper()
# method and checking if both the strings are same or not using the if conditional
# statement.
if(gvn_fststr.upper() == gvn_scndstr.upper()):
    # If it is true, then print "The given first and second strings are same".
    print("The given first and second strings are same")
else:
    # Else print "The given first and second strings are NOT same".
    print("The given first and second strings are NOT same")

Output:

The given first and second strings are same

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply the upper() method to the given string that returns a string with all characters in the upper case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the upper () method.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Apply upper() method to the given string that returns a string with all
# characters in upper case.
# Store it in another variable.
rslt = gvn_str.upper()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the upper() method.
print("The given string after applying upper() method :")
print(rslt)

Output:

Enter some Random String = gooD mornING btechgeeks @!@#123
The given original string : gooD mornING btechgeeks @!@#123
The given string after applying upper() method :
GOOD MORNING BTECHGEEKS @!@#123

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String upper() Method with Examples Read More »

Python String find() Method with Examples

In the previous article, we have discussed Python String expandtabs() Method with Examples
String find() Method in Python:

The find() method looks for the first instance of the specified value.

If the value cannot be found, the find() method returns -1.

The find() method is nearly identical to the index() method, with the exception that the index() method throws an exception if the value is not found.

Syntax:

string.find(value, start, end)

Parameters

value: This is Required. It is a string. The value to look for in the string.

start: This is optional. It is an integer. The starting point for the search. The default value is 0.

end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.

Return Value:

The find() method gives the following integer value:

  • If the value exists within the string, it returns the index of the value’s first occurrence.
  • It returns -1 if a value does not exist within the string.

Examples:

Example1:

Input:

Given string = "hello this is btechgeeks this"
Given value = "this "

Output:

The index of the given value's{ this } first occurrence =  6

Example2:

Input:

Given string = "welcome to python programs to all"
Given value = "to"
start position = 20
end position = 30

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

String find() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Pass the given value as an argument to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks this"
# Give the value as static input and store it in another variable.
gvn_valu = "this"
# Pass the given value as an argument to the find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)

Output:

The index of the given value's{ this } first occurrence =  6
2)With giving start and end positions

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Give the start position as static input and store it in another variable.
  • Give the end position as static input and store it in another variable.
  • Pass the given value, start and end positions as arguments to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python programs to all"
# Give the value as static input and store it in another variable.
gvn_valu = "to"
# Give the start position as static input and store it in another variable.
gvn_strtpositn = 20
# Give the end position as static input and store it in another variable.
gvn_endpositn = 30
# Pass the given value, start and end positions as arguments to the find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

The index of the given value's{ to } first occurrence in the given start and end positions :
27

Method #2: Using Built-in Functions (User Input)

1)Without giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Pass the given value as an argument to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Pass the given value as an argument to the find() function for the given
# string which returns the index of the given value's first occurrence if the
# value exists and returns -1 if a value does not exist within the given string.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence = ", rslt)


Output:

Enter some Random String = good morning btechgeeks
Enter some Random String(value) = e
The index of the given value's{ e } first occurrence = 15
2)With giving start and end positions

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Give the start position as user input using the int(input()) function and store it in another variable.
  • Give the end position as user input using the int(input()) function and store it in another variable.
  • Pass the given value, start and end positions as arguments to the find() function for the given string which returns the index of the given value’s first occurrence if the value exists and returns -1 if a value does not exist within the given string in the given start and end range.
  • Store it in another variable.
  • Print the index of the given value’s first occurrence in the given string in the given start and end positions.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some Random String(value) = ")
# Give the start position as user input using the int(input()) function and 
# store it in another variable.
gvn_strtpositn = int(input("Enter some random number = "))
# Give the end position as user input using the int(input()) function and 
# store it in another variable.
gvn_endpositn = int(input("Enter some random number = "))
# Pass the given value, start and end positions as arguments to the find()
# function for the given string which returns the index of the given value's
# first occurrence if the value exists and returns -1 if a value does not
# exist within the given string in the given start and end range.
# Store it in another variable.
rslt = gvn_str.find(gvn_valu, gvn_strtpositn, gvn_endpositn)
# Print the index of the given value's first occurrence in the given string.
print(
    "The index of the given value's{", gvn_valu, "} first occurrence in the given start and end positions :")
print(rslt)

Output:

Enter some Random String = good morning all
Enter some Random String(value) = o
Enter some random number = 3
Enter some random number = 10
The index of the given value's{ o } first occurrence in the given start and end positions :
6

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String find() Method with Examples Read More »

Python String lower() Method with Examples

In the previous article, we have discussed Python String index() Method with Examples
String lower() Method in Python:

lower() returns a string with all characters in lower case.

Symbols and numbers are not accepted.

Syntax: 

string.lower()

Parameter Values: This method has no parameters.

Return Value:

lower() returns the lowercase string from a given string. It converts all uppercase to lowercase characters.

If no uppercase characters are found, the original string is returned.

Examples:

Example1:

Input:

Given String = "HELLO this IS Btechgeeks"

Output:

The given original string : HELLO this IS Btechgeeks
The given string after applying lower() method :
hello this is btechgeeks

Example2:

Input:

Given String = "GooD MORNING btechgeeks@123$$#"

Output:

The given original string : GooD MORNING btechgeeks@123$$#
The given string after applying lower() method :
good morning btechgeeks@123$$#

String lower() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply the lower() method to the given string that returns a string with all characters in lower case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the lower() method.
  • The Exit of the program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "HELLO this IS Btechgeeks"
# Apply lower() method to the given string that returns a string with all characters
# in lower case.
# Store it in another variable.
rslt = gvn_str.lower()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the lower() method.
print("The given string after applying lower() method :")
print(rslt)

Output:

The given original string : HELLO this IS Btechgeeks
The given string after applying lower() method :
hello this is btechgeeks

How does lower() function work in a program?

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Converting both the given first and second strings into lowercase using the lower() method and checking if both the strings are the same or not using the if conditional statement.
  • If it is true, then print “The given first and second strings are same”.
  • Else print “The given first and second strings are NOT same”.
  • The Exit of the program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "WELCOME to PythoN PROGRAMS"
# Give the second string as static input and store it in a variable.
gvn_scndstr = "Welcome to Python programs"
# Converting both the first and second strings into lowercase using the lower()
# method and checking if both the strings are same or not using the if conditional
# statement.
if(gvn_fststr.lower() == gvn_scndstr.lower()):
    # If it is true, then print "The given first and second strings are same".
    print("The given first and second strings are same")
else:
    # Else print "The given first and second strings are NOT same".
    print("The given first and second strings are NOT same")

Output:

The given first and second strings are same

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply lower() method to the given string returns a string with all characters in lower case.
  • Store it in another variable.
  • Print the given original string.
  • Print the given string after applying the lower() method.
  • The Exit of the program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some Random String = ")
# Apply lower() method to the given string that returns a string with all characters
# in lower case.
# Store it in another variable.
rslt = gvn_str.lower()
# Print the given original string.
print("The given original string :", gvn_str)
# Print the given string after applying the lower() method.
print("The given string after applying lower() method :")
print(rslt)

Output:

Enter some Random String = GooD MORNING btechgeeks@123$$#
The given original string : GooD MORNING btechgeeks@123$$#
The given string after applying lower() method :
good morning btechgeeks@123$$#

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.

Python String lower() Method with Examples Read More »