Wednesday, February 5, 2020

String in Python

Python String


String


In Python, Strings are basically arrays of Unicode character bytes and can be represented by enclosing a sequence of characters within single or double quotes, we can create string literals by simply assigning a string value to a variable, for example

str1=’welcome to Python’
str2=” welcome to dynamic programming”


We can also write multi-line string using a triplet of single or double quotes,

str1='''python,   
is,
super,
programming,
language'''
     

str2="""welcome,
to, 
dynamic,
programming"""

Character data types are not available in Python. It means all the characters declared like 

ch=’c’ 

are actually strings of length equal to one. As strings are arrays we can access the characters at different index positions by using their index itself. We can also use index slicing to access fragment within some given index range. We can also use negative indexing with a string literal.

Try yourself:




Methods


There are several methods available for string handling in Python.
  • len(str)  to get the length of the string.
  • strip(str) to remove white spaces from the start and end of the string.
  • split(str) to split the string.
  • replace(str1,str2) to replace some sequence of characters within the string, with some other string.
  • lower() to change the string into lower cases.
  • upper() to change the string into upper cases. 

Try yourself: