Sitemap
write_avis

write_avis

Beginner data scientist, Beginner writer, sharing my views and weekly reports, mostly tech-related content, reach me at writeavis@gmail.com

PYTHON STRINGS — PART1

Week A17: 24th-02nd Mar2025

4 min readMar 3, 2025

--

Extracted definitions from Internet and Chat-GPT which i felt reasonable

Data , Information, Knowledge

  • Data is raw information that can be stored, processed and analyzed
  • Information is processed, structure and meaningful data
  • Knowledge is insights and conclusions drawn from information

Data type

  • A data type is a way to categorize data so that computer can interpret and perform appropriate operations on it.
  • The primitive data types are : int, float, string, bool, bytes

Data Structure

  • A data structure is a way of organizing and storing data efficiently to enable easy access and modification.
  • examples include : list, tuple, dict, set
  • One of the key reasons behind NumPy’s impressive speed is its highly efficient ndarray data structure.

Class

  • A class is a blue print for creating objects. It defines attributes and methods
  • all built in data types in python are actually implemented as classes unlike other programming languages

String

  • In python string is a built-in data type used to store and manipulate text (precisely Unicode characters)
  • We can also say strings are a type of data structures because they are a sequence of characters which allows indexing, slicing and iteration
  • As mentioned above we can say string is a built-in class
  • every string we create is an instance(object) of str class
  • strings are immutable, so modifying a string creates a new object

We will now see all the things mentioned above that are

  • How to create a string object
  • Indexing, slicing and iteration of string
  • what is meant by immutable
  • We mentioned string as a class so we will also see what are its attributes and methods. (part-2)

Creating Strings

Most common way of creating strings is using quotes

  • single quotes ’string’ (Generally used if strings consists of double quotes)
  • Double quotes ”string” (Generally used if strings consists of single quotes)

similarly triple quotes(”””string””” or ‘’’string’’’) is used for writing multi-line strings or doc strings

  • Triple quotes ”””string””” or ‘’’string’’’ (Generally used if strings consisting both single and double quotes or requirement of multiline characters)

Second way is typecasting one data type or structure to other using **str** keyword

Get write_avis’s stories in your inbox

Join Medium for free to get updates from this writer.

Some other uncommon methods include **repr()**, **join()**, **format()**, **decode()**, **bytes.decode()**

### Lets initiate a string object 
s1 = 'hello "world"'
s2 = "hello 'world'"
s3 = '''"hello"
'world' '''
s4 = """"hello"
'world'"""
print(s1,s2,s3,s4,sep='\n\n')

>>>output
hello "world"

hello 'world'

"hello"
'world'

"hello"
'world'

INDEXING ,SLICING, ITERATION

Iterable

s = 'Python programming'
# string object is iterable
for i in s:
print(i,end= ' ')

>>>output
P y t h o n p r o g r a m m i n g

Indexing

  • In python there are two types of indexing one is positive indexing and other is negative indexing
  • positive indexing starts from 0 and goes 1,2,3….
  • similarly negative indexing starts from -1 and goes -2,-3,-4…..
# defining positve indexes using enumerate funtion and for loop 
for index, character in enumerate(s):
print(f'Index: {index}, Character: {character}')
>>>output
Index: 0, Character: P
Index: 1, Character: y
Index: 2, Character: t
Index: 3, Character: h
Index: 4, Character: o
Index: 5, Character: n
Index: 6, Character:
Index: 7, Character: p
Index: 8, Character: r
Index: 9, Character: o
Index: 10, Character: g
Index: 11, Character: r
Index: 12, Character: a
Index: 13, Character: m
Index: 14, Character: m
Index: 15, Character: i
Index: 16, Character: n
Index: 17, Character: g
# defining -ve indexes using while loop 
index = -1
while index >= -len(s):
print(f'Index: {index}, Character: {s[index]}')
index-=1
Index: -1, Character: g
Index: -2, Character: n
Index: -3, Character: i
Index: -4, Character: m
Index: -5, Character: m
Index: -6, Character: a
Index: -7, Character: r
Index: -8, Character: g
Index: -9, Character: o
Index: -10, Character: r
Index: -11, Character: p
Index: -12, Character:
Index: -13, Character: n
Index: -14, Character: o
Index: -15, Character: h
Index: -16, Character: t
Index: -17, Character: y
Index: -18, Character: P
Press enter or click to view image in full size
image created from excel
# now verifying with random examples 
print(s[0],s[-18],s[5],s[-13],s[10],s[-8],s[16],s[-2],sep = ' ')

>>>output
P P n n g g n n

Slicing

# slicing is extracting a sequence using indexing 

print(f'First 10 letter s[:10]: {s[:10]}')
print(f'Extracting 7 letter starting from t s[2:9]: {s[2:9]}')
print(f'last 7 letter using positive indexing s[len(s)-7:]: {s[len(s)-7:]}')
print(f'last 7 letter using negative indexing s[-7:]: {s[-7:]}')

print(f'Extracting characters at even indexes from a string s[::2]: {s[::2]}')
print(f'Extracting characters at odd indexes from a string s[1::2]: {s[1::2]}')
print(f'Extracting chars where index is divisible by 3 s[::3]: {s[::3]}')

print(f'Reversing a string s[::-1]: {s[::-1]}')

>>>output
First 10 letter s[:10]: Python pro
Extracting 7 letter starting from t s[2:9]: thon pr
last 7 letter using positive indexing s[len(s)-7:]: ramming
last 7 letter using negative indexing s[-7:]: ramming
Extracting characters at even indexes from a string s[::2]: Pto rgamn
Extracting characters at odd indexes from a string s[1::2]: yhnpormig
Extracting chars where index is divisible by 3 s[::3]: Ph oai
Reversing a string s[::-1]: gnimmargorp nohtyP

Immutable

# string is immutable once it is created 
s = 'hello world'
# Editing a string using indexing as shown below will produces error
s[0] = 'H'

>>>output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[51], line 3
1 # string is immutable once it is created
2 s = 'hello world'
----> 3 s[0] = 'H'

TypeError: 'str' object does not support item assignment

--

--

Written by write_avis

Beginner data scientist, Beginner writer, sharing my views and weekly reports, mostly tech-related content, reach me at writeavis@gmail.com