Strings are the simplest and easy to use in Python.
String pythons are immutable.
We can simply create Python String by enclosing a text in single as well as double quotes. Python treat both single and double quotes statements same.
Accessing Strings:
- In Python, Strings are stored as individual characters in a contiguous memory location.
- The benefit of using String is that it can be accessed from both the directions in forward and backward.
- Both forward as well as backward indexing are provided using Strings in Python.
- Forward indexing starts with 0,1,2,3,....
- Backward indexing starts with -1,-2,-3,-4,....
Strings Operators
There are basically 3 types of Operators supported by String:
1.
Basic
Operators.
2.
Membership
Operators.
3.
Relational
Operators.
Basic Operators:
There are two types of basic operators in String. They are "+" and "*".String Concatenation Operator :(+)
The concatenation operator (+) concatenate two Strings and forms a new String.Replication Operator: (*)
Replication operator uses two parameter for operation. One is the integer value and the other one is the String.The Replication operator is used to repeat a string number of times. The string will be repeated the number of times which is given by the integer value.
Membership Operators
Membership Operators are already discussed in the Operators section. Let see with context of String.There are two types of Membership operators:
1) in:"in" operator return true if a character or the entire substring is present in the specified string, otherwise false.
2) not in:"not in" operator return true if a character or entire substring does not exist in the specified string, otherwise false.
Relational Operators:
All the comparison operators i.e., (<,><=,>=,==,!=,<>) are also applicable to strings. The Strings are compared based on the ASCII value or Unicode(i.e., dictionary Order).Slice Notation:
String slice can be defined as substring which is the part of string. Therefore further substring can be obtained from a string.There can be many forms to slice a string. As string can be accessed or indexed from both the direction and hence string can also be sliced from both the direction that is left and right.
String Functions and Methods:
There are many predefined or built in functions in String. They are as follows:
capitalize()
|
It capitalizes the first character of the String.
|
count(string,begin,end)
|
Counts number of times substring occurs in a
String between begin and end index.
|
endswith(suffix ,begin=0,end=n)
|
Returns a Boolean value if the string terminates
with given suffix between begin and end.
|
find(substring ,beginIndex, endIndex)
|
It returns the index value of the string where
substring is found between begin index and end index.
|
index(subsring, beginIndex, endIndex)
|
Same as find() except it raises an exception if
string is not found.
|
isalnum()
|
It returns True if characters in the string are
alphanumeric i.e., alphabets or numbers and there is at least 1 character.
Otherwise it returns False.
|
isalpha()
|
It returns True when all the characters are
alphabets and there is at least one character, otherwise False.
|
isdigit()
|
It returns True if all the characters are digit
and there is at least one character, otherwise False.
|
islower()
|
It returns True if the characters of a string are
in lower case, otherwise False.
|
isupper()
|
It returns False if characters of a string are in
Upper case, otherwise False.
|
isspace()
|
It returns True if the characters of a string are
whitespace, otherwise false.
|
len(string)
|
len() returns the length of a string.
|
lower()
|
Converts all the characters of a string to Lower
case.
|
upper()
|
Converts all the characters of a string to Upper
Case.
|
startswith(str ,begin=0,end=n)
|
Returns a Boolean value if the string starts with
given str between begin and end.
|
swapcase()
|
Inverts case of all characters in a string.
|
lstrip()
|
Remove all leading whitespace of a string. It can
also be used to remove particular character from leading.
|
rstrip()
|
Remove all trailing whitespace of a string. It can
also be used to remove particular character from trailing.
|
Python List
1).Python lists are the data structure that is capable of holding different type of data.2).Python lists are mutable i.e., Python will not create a new list if we modify an element in the list.
3).It is a container that holds other objects in a given order. Different operation like insertion and deletion can be performed on lists.
4).A list can be composed by storing a sequence of different type of values separated by commas.
5).A python list is enclosed between square([]) brackets.
6).The elements are stored in the index basis with starting index as 0.
List Operations:
Various Operations can be performed on List. Operations performed on List are given as:a) Adding Lists:
Lists can be added by using the concatenation operator(+) to join two lists.
Functions and Methods of Lists:
There are many Built-in functions and methods for Lists. They are as follows:There are following List functions:
Function
|
Description
|
min(list)
|
Returns the minimum value from the list given.
|
max(list)
|
Returns the largest value from the given list.
|
len(list)
|
Returns number of elements in a list.
|
cmp(list1,list2)
|
Compares the two list.
|
list(sequence)
|
Takes sequence types and converts them to lists.
|
No comments:
Post a Comment