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, and 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 role in the field of
- Media and communication
- Emotion detection System
- Textual analysis
Python is very rich programming language in term 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.
Open Source
Python is open source. It means there is no need to pay money for 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 short period of time. In this way Python increases the productivity of programmers.
Scripting Language
Python code is translated by interpreter , hence showing errors line by line.
Rich Library of built in functions
There is a 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 large extent.
Platform Independent
Python is Platform Independent. It means Python Programs can be executed on various Operating Systems.
Due to above features python has become No. 1 programming language. That’s why most of the 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 now a days, text/strings are analyzed to obtain meaning 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 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 machine to obtain the 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 perform operations on variables and values.
Python divides the operators in 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 −
Operator | Description | Example |
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 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 −
Operator | Description | Example |
== | If the values of two operands are equal, then the condition becomes true. | (x == y) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | (x != y) is true. |
<> | If values of two operands are not equal, then condition becomes true. | (x <> y) is true. This is similar to != operator. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (x> y) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | (x < y) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (x >= y) is not true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | (x <= y) is true. |
Python Assignment Operators
Operator | Description | Example |
= | Assigns values from right side operands to left side operand | c = x +y assigns value of x + y into c |
+= Add AND | It adds right operand to the left operand and assign the result to left operand | c += x is equivalent to c = c + x |
-= Subtract AND | It subtracts right operand from the left operand and assign the result to left operand | c -= x is equivalent to c = c – x |
*= Multiply AND | It multiplies right operand with the left operand and assign the result to left operand | c *= x is equivalent to c = c * x |
/= Divide AND | It divides left operand with the right operand and assign the result to left operand | c /= x is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assign the result to 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 assign value to the left operand | c //= x is equivalent to c = c // x |
Python Bitwise Operators
Bitwise operator works on bits and performs 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.
Following table lists out the bitwise operators supported by Python language with an example each in 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
Operator | Description | Example |
& 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 Ones 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 following logical operators supported by Python language. Assume variable x holds 10 and variable y holds 20 then
Operator | Description | Example |
and Logical AND | If both the operands are true then condition becomes true. | (x and y) is true. |
or Logical OR | If any of the two operands are non-zero then 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
Operator | Description | Example |
In | Evaluates to true if it finds a variable in the specified sequence and false otherwise. | x in y, here in results in a 1 if x is a member of sequence y. |
not in | Evaluates to true if it does not finds 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 −
Operator | Description | Example |
Is | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. | x is y, here is results in 1 if id(x) equals id(y). |
is not | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. | x is not y, here is not 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.
for more technical article click on the link
Pingback: Python Program to Find the Fibonacci Series and Factorial of a Number - Database Management System | SQL Tutorials