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.
Table of Contents
Easy to learn
Software companies can train their fresher candidates easily and quickly to make them ready for projects.
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.
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.
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
- 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
mystring=”HELLO THIS IS PYTHON”
print(“No of characters “,len(mystring))
output: No of characters 20
String Operations-String Module
- import string
- #returning all letters
- #returning lowercase letters
- #returning uppercase letters
- #returning all punctuations
- #returning whitespaces
- #returning all digits
String Operation more
- st=“hello world”
- st=st.capitalize()#”’capitalizes only first letter”’
- print(st)#”’capitaizes all words”’
- st=st.lower()#”’covert string to lower case”’
- st=st.upper()#”’convert in uppercase”’
- if st.isupper():
- if st.islower():#”’checks whether all characters are lower case”’ print(“True”)
- if st.isalpha():
- st=“Hello World”
- x=st.istitle()#”’check whether string is a title”’
- st=“hello world”
- 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:
Format specifier %s for string
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 −
|Addition(+)||This operator is used to perform addition||x + y = 30|
|Subtraction(-)||This operator is used to perform subtraction.||x – y = -10|
|Multiplication(*)||This operator is used to perform multiplication||x * y = 200|
|Division(/)||This operator is used to perform division||y/ x = 2|
|Modulus(%)||This operator is used to compute the remainder||y % x = 0|
|Exponent(**)||Performs exponential (power) calculation on operators||x**y =10 to the power 20|
|//||Floor Division – This operator gives only integer part of the Result after division||9//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 −
|==||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
|=||Assigns values from right side operands to left side operand||c = x +y assigns value of x + y into c|
|+= Add AND||It adds the right operand to the left operand and assigns the result to the left operand||c += x is equivalent to c = c + x|
|-= Subtract AND||It subtracts the right operand from the left operand and assigns the result to the left operand||c -= x is equivalent to c = c – x|
|*= Multiply AND||It multiplies the right operand with the left operand and assigns the result to the left operand||c *= x is equivalent to c = c * x|
|/= Divide AND||It divides the left operand with the right operand and assigns the result to the left operand||c /= x is equivalent to c = c / a|
|%= Modulus AND||It takes modulus using two operands and assigns the result to the left operand||c %= x is equivalent to c = c % x|
|**= Exponent AND||Performs exponential (power) calculation on operators and assign value to the left operand||c **= x is equivalent to c = c ** x|
|//= Floor Division||It performs floor division on operators and assigns value to the left operand||c //= 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
|& Binary AND||Operator copies a bit to the result if it exists in both operands||(x & y) (means 0000 1100)|
|| Binary OR||It copies a bit if it exists in either operand.||(x | y) = 61 (means 0011 1101)|
|^ Binary XOR||It copies the bit if it is set in one operand but not both.||(x ^ y) = 49 (means 0011 0001)|
|~ Binary One’s Complement||It 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 Shift||The left operands value is moved left by the number of bits specified by the right operand.||x << 2 = 240 (means 1111 0000)|
|>> Binary Right Shift||The 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
|and Logical AND||If both the operands are true then the condition becomes true.||(x and y) is true.|
|or Logical OR||If any of the two operands are non-zero then the condition becomes true.||(x or y) is true.|
|not Logical NOT||Used 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
|In||Evaluate 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 in||Evaluate 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 −
|Is||Evaluate 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 not||Evaluate 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).|
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.