Python String

In this page you learn about of Strings are easy and most simple to implement in Python. Strings in python are unchangeable or immutable.

We can essentially make Python Enclosing so as to string content in single and additionally twofold quotes. Python treat both single and twofold quotes explanations same.

Getting to Strings

In Python, Strings are put away as individual characters in a touching memory area. The advantage of utilizing String is that it can be gotten to from both the directions in forward and backward. Both forward and also backward indexing are given utilizing Strings as a part of Python.

Now forward indexing begins with 0,1,2,3... And Backward indexing begins with - 1, - 2, - 3, - 4...

python-string

str[0]='P'=str[-6] , str[1]='Y' = str[-5]  ,  str[2] = 'T' = str[-4]  ,  str[3] = 'H' = str[-3]  
str[4] = 'O' = str[-2]  ,  str[5] = 'N' = str[-1].  
 
name="Mithilesh"  
length=len(name)  
i=0  
for n in range(-1,(-length-1),-1):  
    print name[i],"\t",name[n]  
    i+=1  
    

Strings Operators

There are fundamentally 3 sorts of Operators bolstered by String

  • Basic Operators
  • Membership Operators
  • Relational Operators

Basic Operators

There are two sorts of essential administrators in String. They are "+" and "*".


Membership Operators

There are two type membership operators like this

  • in
  • not in

Relational Operators

All the comparison operators like, (<,><=,>=,==,!=,<>) are also applicable to strings. The Strings are compared based on the ASCII value or Unicode(i.e., dictionary Order)


String Functions and Methods:

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.