Sitemap
Manchirala Arun Kumar

Manchirala Arun Kumar

String Data Type of Python

3 min readJun 9, 2024
Press enter or click to view image in full size

String Data type :

It is a sequence of characters which is enclosed inside a double quotes or single quotes & triple quotes.

Properties of string data type :

  1. String can have any single or a sequence of character { [0–9] , [A-Z, a-z], [Special symbol] }.

2. Strings are immutable. (Once you create the data of string no change is possible ).

  • You cannot add a new character and delete , replace any existing character.

3. Inside the string each and every character or element will have an Index Value = > Address .

  • For every character or element having two index value.
  1. Positive index value
  2. Negative index value
  3. Positive index value :

Whenever you go from left to right inside the string it is known as Positive index value.

2. Negative Index value:

Whenever you go from right to left inside the string it is known as Negative index value.

Indexing : To access Any single character inside a string is called indexing.

  • It is a technique by using which we can access the single character.
  • Example 1 (+ve ):- var1 = “ a b c d e” ( var1[1] ===> it returns the value ‘b’ from a string.
  • Example 2 (-ve) :- var1 =” a b c d e” (var1[-4] ===> it returns the value ‘b’ from a string.

Slicing : To access Any sequence of characters inside a string is called Slicing.

Slicing Syntax : Var-name[Starting index : Ending index ]

Positive Slicing:

Whenever you go from left to right to extract a sequence of characters from a string is called Positive Slicing.

Example : var1 = “ a b c d “ ( var1[1 : 2] ====> it returns the value “b” )

  • Important Note 1: Ending index will never be included when we are accessing sequence of element.
  • Important Note 2: Machine will always execute from left to right .

Empty String :

Whenever we are having a string which is surrounded by quotes is called empty string.

syntax : x = (“ “)

sub-string :

whenever we are accessing part of a string that is called sub-string.

Example :- (“abcd”)

String Methods :

It is a method which can be applied on string data is called string methods.

  • String methods can have their own functionality to perform specific tasks on strings.

Example : x =”abc” [ x.method() ] ===>

  1. It checks the data whether it is a string or not .
  2. Apply to the functionality to the string data
  3. Lower() :

It will convert any upper case character inside a string into a lower case and i will ignore all the elements which are not alphabets/lower case.

Ex :- x = “Abc1B2Q”

Get Manchirala Arun Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

x.lower()

output :- ‘abc1b2q’

2. Upper():

It will convert any lower case character inside a string into a upper case and i will ignore all the elements which are not alphabets/upper case.

Ex :- x = “Abc1B2Q”

x.upper()

output :- ‘ABC1B2Q’

3. Capitalize():

It will convert the first alphabet into upper case and rest of the alphabets will be converted into lower case and any number will be ignored.

Ex :- x = “abc1B2Q”

x.capitalize()

output :- ‘Abc1b2q’

4. Title():

it will convert first alphabet of each and every words to upper and rest of the alphabets to lower case.

Ex:- var1 =”Ab2ab34ac”

var1.title()

output:-’Ab2Ab34Ac’

5. Count():

It will help you to count a single character or a sequence of character occurred in a string .

Ex :- var3 = “aaaaaaaaaaaabaaaababaaaaa”

var3.count(“aaa”)

output:- 6

When i want to check the count part of string we will use indexing.

syntax: [variablename. count(string,starting index,ending index)]

Ex:- var3 =”abaacd”

var3.count(“a”,1,4)

output: 2

6. Find():

Find method will give you the first occurrence of the specified string (index position)

Ex:- x = “bbacdbbbdbbb”

x.find(“bbb”)

output : 5

  • Find method will not return negative number only it will give positive index.

Syntax :- find(substring, starting index , ending index )

Ex:- x = “ababaa”

x.find(“a”)

output : 2

Index():

This method is similar to find method .

Index will tell you the index value of first occurrence.

Difference between index and find methods:-

whenever the sub-string is not present in string find will return [ -1 ] and index will return an error.

--

--