Category Archives: Variables & Datatypes

Strings:-Built-in Functions

Python provides some following built-in methods to manipulate strings:-
(I). capitalize() :-
It returns the string with only its first character capitalized.
As:-
Untitled
(II). isdigit() :-
It returns True if the string contains only digits,False otherwise.
As:-
Untitled
(III).  isalnum() :-
 
If string contains alphanumric characters with atleast one character,it returns True.False otherwise.As:-
Untitled
(IV). isalpha() :-
It checks whether the string contains alphabetic characters only or not. Returns accordingly True & False. As:-
Untitled
(V). len():-
This is used to find the length of the string. As:-
Untitled
(VI). isupper() :-
this method checks if the string consists with uppercase letter or not.Returns value accordingly True or False. As:-
Untitled
                                          islower() also works as isupper but returns lower case.
(VII). lower() :-
It converts all the characters of string in lowercase form.As:-
Untitled
(VIII). upper() :-
It converts all the characters of string in uppercase form.As:-
Untitled
(IX).  max() :-
Using this you can find,maximum character of a string(max means which is having highest ASCII value) .
(X). min() :-
It is as max() but returns min character according to their ASCII value.
both can be used As:-Untitled
(XI). swapcase() :-
This function is used to swap the case.As:-
Untitled
(XII). replace() :-
This function is used to replace old copy of the string with new. There are three parameters named as old,new &  tim ( how many time you want to make changes) now the syntax for replace() will be str.replace(“old”, “new”, tim). but default(without tim) it will replace all occurrences.As:-
Untitled

(XIII). find() :-
It tells the the occurrence of a substring in a string.return index if found otherwise -1. This function required three parameters substr,beg & end (beg & end are values from where to where we have to find).By default beg is 0 and end is the length of the string.Now syntax is str.find(substr,beg,end) . As:-
Untitled
(XIV). index() :-
This is same as find but gives error when substring is not found.Syntax is also same as find index(substr,beg,end).
Untitledby default beg is set on 0 and end = len(str).

(XV). rfind() & rindex() :-
Both are same as find() and index() respectively  but difference is that it returns last index where the substr is found otherwise -1( in case of rfind() ) and error ( in case of rindex() ).
As:-
Untitled
(XVI). split() :-
This function works as a separator and makes list of all words.Two parameters are
required str and tim. tim(number of time).By default str is whitespace.As:-
Untitled
(XVII). join() :-
this method merge the string between the provided sequence .As shown here:-
Untitled
(XVIII). count() :-
This method tells us how many time substr occur in a string.Syntax is str.count(substr,beg,end). By default beg =0 , and end = len(str).As:-
Untitled

Datatypes:- Strings

Python strings:-
a set of characters in between quotation marks are called as strings.We can use single quotes or double quotes to represent strings.As:-
Untitled

We can access individual characters using indexing and a range of characters using slicing.Negative indexing is allowed in python( [-1] will be treated as last character and so on). As shown here:-
Untitled

String Operations:-
There are many operations that can be performed with string are given below:-
1. Concatenation:-
The ( + ) operator is used to concatenate two or more string. Using parentheses we can also concatenate two strings. The asterisk ( * ) is the repetition operator.As shown here:-
 Untitled
Updating string:-
we can update a string as shown here.:-
~~~~
>>>  name = "Hello"
>>>  name[:6] + " Python..!!"
'hello Python..!!'
~~~~

Membership test:-
We can test if a sub string exists within a string or not, using the keyword “in” & “not in” .
As shown here:-
Untitled

Data Types:-

There are various datatypes in Python that are used to define the operations possible on them and the storage method for each of them.As given below:-
ï‚· Numbers
ï‚· String
ï‚· List
ï‚· Tuple
ï‚· Dictionary

Python Numbers:-
Integers, floating point numbers and complex numbers comes under Python numbers category.They are defined as int, float and complex  class in Python,by using type() function, we can know which class a variable or a value belongs to and the isinstance() function to check if an object belongs to a particular class. As shown here:-Data types
Integers can be of any length but floating number can be up to 15  decimal places.Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.
we can delete a single or multiple objects by using the del statement,As:-
Untitled

Type Conversion:-

We can convert one type of number into another by using built in function  like int(),float() and complex(). As shown here:-
Untitled

Variables:-

A variable is a location in memory used to store some data.In Python, We don’t need to declare a variable before using it. We simply assign a value to a variable and it will exist. We don’t even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable.The equal sign (=) is used to assign values to variables.
As:-

name = "Myprogworld"
num1 = 100
num2 = 5.4

print (name)
print(num1)
print (num2)

This will produce following result:-

Myprogworld
100
5.4

Multiple Assignment:-
Python allows multiple assignment in a single statement as follows:-

a,b,c=1,2,3

result:-
multiple variablesIf we want to assign the same value to multiple variables at once, we can do this as:-

a=b=c = 1