String in Python

In Python Programming language, Strings are arrays of bytes constituting unicode characters. Python uses type string for text, which can also be denoted as ‘str’ or sequence of characters. Python doesn’t have any specific character type, a single character is a string of length 1.

Python uses names to recall values. These are actual quantity that we manipulate in our program. Values are stored in names. It consist of some types which allows values to determine what operations are allowed.

Basic Numeric Types-

1.Logical type:- It shows output in True or False form. int, float and bool are the logical types.

2. Boolean type:- In numeric types,using arithmetic operations are complex. We have and, or, not operators which let us handle True and False values.

3. Comparison types:- We can check related values of b2 different quantities using equal to ‘==’, greater than ‘>’, less than ‘<‘, not equal to “!=” operators.

In Python, we can’t say that i is of int type, x is of float type i.e, names don’t have their fix types. It depends on what value you are assigning to them and if you use names without assigning them any values it will give you some error. In python, we do not call names as we do in other programming languages, so before using any new name, we must use that name in assignment statements in LHS.

Text Manipulation

Numeric types are the only things which seems absorbing nowadays in computation. Text Processing is also equally significant because whatever computation we perform is actually dealing with text. While manipulating data, data comes from multiple sources and for further handling we will export it from spreadsheet which is what we say as Text Processing. This is a very influential part of computation and using Python programming language it is very easy to control text where we can program many things.

str type

Values of string type are depicted as we normally do using single, double and triple quotes.

Triple quotes

These quotes are generally used when you wanted to combine both double and single quotes in string.

>>> q = 'Meena' 
>>> q                                                                                                         
'Meena'                 //output//

#this was a normal string using single quotes.

>>> story = '''"I wasn't there"'''            
>>> story
'"I wasn\'t there"'           //output//

>>> story = '''my name is qwe
... my name is uio
... my name is pdf
... i welcome you here'''
>>> story
'my name is qwe\nmy name is uio\nmy name is pdf\ni welcome you here'             //output//

# \n indicates new line. Strings characters have positions like 0, 1, 2, ——–, n-1.

>>> q = 'Mumbai'
>>> q[1]
'u'                 //output//

# if we want to count these from backwards, then start labelling from -1, -2, -3, -4, —-.

>>> q[-2]
'a'             //output//

# Square brackets are used to take out seperate position, if we want to know just a single character we specify the position.

Concatenation

Concatenation is combination of 2 values into a large string, ‘ +’ operator is used to put strings one after the other.

>>> q = 'Mumbai'
>>> w = q + ",city"
>>> w
'Mumbai,city'            //output//

# len(s) is used to return the length of s. It denotes total number of characters.

Slicing concept

Slicing is a piece of string used to draw out parts of string. We will take a list of characters, then for extracting certain parts,

>>> q[2:4]
'mb'              //output; it will start at position 2 but will end at position 3.//

# Some shortcuts can be used, if you want to start with the very beginning position, instead of using q[0 : j] you can use it like this-

>>> q[:4]
'Mumb'            //output//

# instead of using q[i : len(q)] yoy may use-

>>> q[4:]
'ai'              //output//

Range

Using this function, we will write +1 in range(1, m+1), this is to avoid writing -1. All ranges are one small of RHS of range.

>>> q[3:8]
'bai' //output//

# here, we dont have 8th position, so he will end up giving the last position instead of error.

Altering Strings

We cannot modernize strings . Once the string is declared later we cannot replace it with some other character. for eg:-

>>> q[3] = "p"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment    //output//

Python does not allow us to do this. So, just try using slices and concatenation only. You can build a new string from old string but replacing the existing string with some characters is not possible. Thus, strings are immutable values.

Leave a comment