Python strings |lowercase in python|comparison operators

This article reveals the power of python language to deal with strings, to perform various operations on strings such as how to get strings to be all lowercase in python, how many characters does a string has in python, and comparison operators.

Apart from all that few mathematical functions will also be discussed such as how to compute the absolute value etc.

String processing and text analysis are playing vital roles in the field of

  • Media and communication
  • Emotion detection System
  • Textual analysis

Python is a very rich programming language in terms of its features.

Easy to learn

Software companies can train their fresher candidates easily and quickly to make them ready for projects.

Open Source

Python is open source. It means there is no need to pay money for a license. Python is free.

Python can be downloaded from a website www.python.org for free.

Rapid Application Development

Using Python it is possible to develop huge software in less time. Python code is easy and very compact.

Hence developers can develop complex software in a short period of time. In this way, Python increases the productivity of programmers.

Scripting Language

Python code is translated by an interpreter, hence showing errors line by line.

Rich Library of built in functions

There are plenty of in-built functions. So, instead of developing logic for everything, developers can use several in-built functions to full fill their needs to a large extent.

Platform Independent

Python is Platform Independent. It means Python Programs can be executed on various Operating Systems.

Due to the above features python has become the No. 1 programming language. That’s why most people want to learn Python.

So, As a beginner, one should know the basics of python such as

  • Variables
  • Operators
  • Strings etc.

What is Text /String analysis?

There is much more to do with strings because nowadays, text/strings are analyzed to obtain meaningful results.  

Textual analysis is a process of analyzing the words, symbols present in the text to extract the target words/symbols in order to recognize the thinking of the writer.

Why Text/String analysis?

People use their vocabulary to express their thoughts in their text according to their knowledge about the language. So the text given by a person may be unstructured.

The basic purpose of text/String Analysis is to obtain structured data from unstructured data provided by the person. This structured data can be now read by the machine to obtain meaningful targeted information.

Python built in functions to deal with strings.

  • A string is a sequence of letters (called characters).
  • In Python, strings start and end with single or double quotes.

how to get strings to be all lower case in python

Function Name:- lower()

This function takes zero argument

How many characters does a string have in python.

Function Name:- len()

This function takes zero argument

Program:-

mystring=”HELLO THIS IS PYTHON”

print(“No of characters “,len(mystring))

output: No of characters  20

Substring Python

String Operations-String Module

  • import string
  • #returning all letters
  • print(string.ascii_letters)
  • #returning lowercase letters
  • print(string.ascii_lowercase)
  • #returning uppercase letters
  • print(string.ascii_uppercase)
  • #returning all punctuations
  • print(string.punctuation)
  • #returning whitespaces
  • print(string.whitespace)
  • #returning all digits
  • print(string.digits)

String Operation more

  • st=“hello world”
  • st=st.capitalize()#”’capitalizes only first letter”’
  • print(st)
  • st=st.title()
  • print(st)#”’capitaizes all words”’
  • st=st.lower()#”’covert string to lower case”’
  • print(st)
  • st=st.upper()#”’convert in uppercase”’
  • print(st)
  • if st.isupper():
  •     print(True)
  • st=“hello”
  • x=st.islower()
  • print(x)
  • if st.islower():#”’checks whether all characters are lower case”’ print(“True”)
  • st=abc~123″
  • if st.isalpha():
  • print(“true”)
  • st=“Hello World”
  • x=st.istitle()#”’check whether string is a title”’
  • print(x)
  • st=“4564”
  • x=st.isdigit()
  • print(x)
  • st=“hello world”
  • print(len(st))
  • Rsplit. Usually rsplit() is the same as split. The only difference occurs when the second argument is specified. This limits the number of times a string is separated.
  • So:When we specify 3, we split off only three times from the right. This is the maximum number of splits that occur. # Data.
  • s = “Buffalo;Rochester;Yonkers;Syracuse;Albany;Schenectady”
  • # Separate on semicolon.
  • # … Split from the right, only split three.
  • cities = s.rsplit(“;”, 3)
  • # Loop and print.
  • for city in cities:
  •  print(city)

Format specifier %s for string

s1=”hello world”
print(“The result is %s “%s1)

Output:- The result is hello world

Python absolute value and more

#math module
import math
# absolute value of -50
print(abs(-50))#output 50
print(math.pow(2, 3))
print(math.factorial(5))
print(math.sqrt(25))
print(math.ceil(3.9876))
print(math.floor(3.7679))
print(round(5.1))
print(max(1,2,3))
print(min(1,2,3))

Python comparison operator and more

Operators are used to performing operations on variables and values.

Python divides the operators into the following groups:

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Python Arithmetic Operators

Assume variable x holds 10 and variable y holds 20, then −

OperatorDescriptionExample
Addition(+)This operator is used to perform additionx + y = 30
Subtraction(-)This operator is used to perform subtraction.x – y = -10
Multiplication(*)This operator is used to perform multiplicationx * y = 200
Division(/)This operator is used to perform divisiony/ x = 2
Modulus(%)This operator is used to compute the remaindery % x = 0
Exponent(**)Performs exponential (power) calculation on operatorsx**y =10 to the power 20
//Floor Division – This operator gives only integer part of the Result after division9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0

Python Comparison Operators

  • These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.
  • Assume variable x holds 10 and variable y holds 20, then −
OperatorDescriptionExample
==If the values of two operands are equal, then the condition becomes true.(x == y) is not true.
!=If the values of two operands are not equal, then the condition becomes true.(x != y) is true.
<> If the values of two operands are not equal, then the condition becomes true.(x <> y) is true. This is similar to != operator.
If the value of the left operand is greater than the value of the right operand, then the condition becomes true.(x> y) is not true.
If the value of the left operand is less than the value of the right operand, then the condition becomes true.(x < y) is true.
>=If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true.(x >= y) is not true.
<=If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true.(x <= y) is true.

Python Assignment Operators

OperatorDescriptionExample
=Assigns values from right side operands to left side operandc = x +y assigns value of x + y into c
+= Add ANDIt adds the right operand to the left operand and assigns the result to the left operandc += x is equivalent to c = c + x
-= Subtract ANDIt subtracts the right operand from the left operand and assigns the result to the left operandc -= x is equivalent to c = c – x
*= Multiply ANDIt multiplies the right operand with the left operand and assigns the result to the left operandc *= x is equivalent to c = c * x
/= Divide ANDIt divides the left operand with the right operand and assigns the result to the left operandc /= x is equivalent to c = c / a
%= Modulus ANDIt takes modulus using two operands and assigns the result to the left operandc %= x is equivalent to c = c % x
**= Exponent ANDPerforms exponential (power) calculation on operators and assign value to the left operandc **= x is equivalent to c = c ** x
//= Floor DivisionIt performs floor division on operators and assigns value to the left operandc //= x is equivalent to c = c // x

Python Bitwise Operators

Bitwise operator works on bits and performs the bit-by-bit operation. Assume if x = 60; and y= 13; Now in the binary format their values will be 0011 1100 and 0000 1101 respectively.

The following table lists out the bitwise operators supported by Python language with an example of each of those, we use the above two variables (x and y) as operands −

  • x = 0011 1100
  • y = 0000 1101
  • —————–
  • x&y = 0000 1100

  • x|y = 0011 1101
  • x^y = 0011 0001
  • ~x  = 1100 0011
  • There are following Bitwise operators supported by Python language
OperatorDescriptionExample
& Binary ANDOperator copies a bit to the result if it exists in both operands(x & y) (means 0000 1100)
| Binary ORIt copies a bit if it exists in either operand.(x | y) = 61 (means 0011 1101)
^ Binary XORIt copies the bit if it is set in one operand but not both.(x ^ y) = 49 (means 0011 0001)
~ Binary One’s ComplementIt is unary and has the effect of ‘flipping’ bits.(~x ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number.
<< Binary Left ShiftThe left operands value is moved left by the number of bits specified by the right operand.x << 2 = 240 (means 1111 0000)
>> Binary Right ShiftThe left operands value is moved right by the number of bits specified by the right operand.x >> 2 = 15 (means 0000 1111)

Python Logical Operators

There are the following logical operators supported by the Python language. Assume variable x holds 10 and variable y holds 20 then

OperatorDescriptionExample
and Logical ANDIf both the operands are true then the condition becomes true.(x and y) is true.
or Logical ORIf any of the two operands are non-zero then the condition becomes true.(x or y) is true.
not Logical NOTUsed to reverse the logical state of its operand.Not(x and y) is false.

Python Membership Operators

Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below

OperatorDescriptionExample
InEvaluate to true if it finds a variable in the specified sequence and false otherwise.x in y herein results in a 1 if x is a member of sequence y.
not inEvaluate to true if it does not find a variable in the specified sequence and false otherwise.x not in y, here not in results in a 1 if x is not a member of sequence y.

Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity operators explained below −

OperatorDescriptionExample
IsEvaluate to true if the variables on either side of the operator point to the same object and false otherwise.x is y, here it results in 1 if id(x) equals id(y).
is notEvaluate to false if the variables on either side of the operator point to the same object and true otherwise.x is not y, there are no results in 1 if id(x) is not equal to id(y).

Conclusion:

After having the knowledge of the above-mentioned built-in functions regarding string, one can easily play with strings. Python built-in functions of string can be used in text analysis and string processing.

1 thought on “Python strings |lowercase in python|comparison operators”

Leave a Comment