DEV Community

Cover image for Basics of Python "String"
Umme Abira Azmary
Umme Abira Azmary

Posted on

Basics of Python "String"

String

String is a sequence of ordered characters (alphabets-lower case, upper case, numeric values, special symbols).

Strings are either enclosed with single qutation marks('),double quotation marks('') and triple quotation marks('''). Also we can write multi line string using triple qutations(''', " " " both) The character order is always from left to right.

print("Hello! I am mouly")
print('Helloo! I am Mouly')
print('''This is
a multi line 
string and we
call it "Doc-Strings" ''')
Enter fullscreen mode Exit fullscreen mode

Output:

Hello! I am mouly
Helloo! I am Mouly
This is
a multi line 
string and we
call it "Doc-Strings" 
Enter fullscreen mode Exit fullscreen mode

we can use either single quotation or double quotation as long as we are consistant about which qutation we are using

print("He was in the dead sea while listening 'Ocean Eyes'")
Enter fullscreen mode Exit fullscreen mode

Output:

He was in the dead sea while listening 'Ocean Eyes'
Enter fullscreen mode Exit fullscreen mode

This qutation sequence can easily be maintained by using preceding backslash which is called as an escape character( \ )

print('I manage because I have to. Because I\'ve no other way out')
print("Don't you know,\"How can you ignore?\"")
Enter fullscreen mode Exit fullscreen mode

Output:

I manage because I have to. Because I've no other way out
Don't you know,"How can you ignore?"
Enter fullscreen mode Exit fullscreen mode

An empty string also holds an indexing position

print(" ")                                     #empty string
print(' ')
Enter fullscreen mode Exit fullscreen mode

Output:



Enter fullscreen mode Exit fullscreen mode

f-string

f-strings are string literals called as “formatted string literals” that have an f at the beginning and curly braces containing expressions that will be replaced with their values.

wrd = 'looking'
who ='kid'
print(f'Here\'s {wrd} at you, {who}')
Enter fullscreen mode Exit fullscreen mode

Output:

Here's looking at you, kid
Enter fullscreen mode Exit fullscreen mode

Type

this built-in fuction returns the type of an object

name = "alan walker"  
print(type(name))                             #type of name variable
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Also,

p = "5"                                      #this is a string
q = 5                                        #this is an integer
print(type(p))
print(type(q))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'str'>
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Even though p and q might look same to you.. their types are different and there are different consequences for this.

p = "5"
q = 5
print(p)
print(q + 45)
Enter fullscreen mode Exit fullscreen mode

Output:

5
50
Enter fullscreen mode Exit fullscreen mode

but

p = "5"
q = 5
print(p + 45)                                 #this will give an error
print(q)
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

Here, p is a string. Even though 5 happens to look like a number, in python it's just a sequence of characters and we can't add a number to a sequence of characters.
we can add them if we cast this p string into an integer.

p = "5"
q = 5
print(int(p) + 45))                          #this won't give an error
print(q)
Enter fullscreen mode Exit fullscreen mode

Output:

50
5
Enter fullscreen mode Exit fullscreen mode
p = "5"
print(p + "45")                              #string concetenation
Enter fullscreen mode Exit fullscreen mode

Output:

545
Enter fullscreen mode Exit fullscreen mode

When we call a int of float to cast a string it needs to be a valid number.

p = "20 taka"
print(p + 25)
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

So, python can't convert this string into integer.

Strings are sequential collection datatype.This means a string is actually a collection of single characters.

Indexing

We can access an individual character of a string or part of a string using the indexing operator.
For accessing individual character by it's position or index value. This index value always begins at zero

Indexing can be done in two ways:

Positive Indexing is used to access characters from the left side of a string and it always starts from 0 and ends at the last character of the string.

Negative Indexing is used to access characters from the right side of a string and it starts from -1 and ends at the first character of the string.
String indexing

REMEMBER, A string with six characters have entities from 0 through 5. So if we want to access a 5th character of a string we'll use an index of 4.

The basic string indexing structure

string_name[index_value]
Enter fullscreen mode Exit fullscreen mode
xmple = "we want to access"
print(xmple[0])
print(xmple[2])   
print(xmple[8])                                  #positive indexing
print(xmple[-1])                                 #negative indexing
print(xmple[18])                                 #index out of range
print(xmple[1.5])                                #type error
Enter fullscreen mode Exit fullscreen mode

Output:

w

t
s
IndexError
TypeError
Enter fullscreen mode Exit fullscreen mode

the built-in function len() helps us determine the length of a string. So the last index of a stirng will always be one less than the length of that string.

len(string)
Enter fullscreen mode Exit fullscreen mode
xmple = "we want to access"
print(len(xmple))                               #length of a string
Enter fullscreen mode Exit fullscreen mode

Output:

17
Enter fullscreen mode Exit fullscreen mode

If we want to access last character of a string we can do either of them from below:

xmple = "we want to access"
print(xmple[len(xmple)-1])
print(xmple[-1])
Enter fullscreen mode Exit fullscreen mode

Output:

e
e
Enter fullscreen mode Exit fullscreen mode

Slicing

Slicing is used for getting a substring of a particular string.This allows us to create a sub-string that is more than one character long. Colon(:) is used as a slicing operator.

Keep in mind that, the slice operator leaves the original operator intact.

Basic structure of slicing

string_name[beginning : end : step_size]
Enter fullscreen mode Exit fullscreen mode

beginning: The index where slicing starts (inclusive). If not provided, by default starts from index 0.

end: The index where slicing stops(Not inclusive). If not provided, by default includes the rest of the string after “beginning”.

step : increment of the index value. If not provided, by default the value is 1.

xmple = "we want to access"
print(xmple[1:9:1])
Enter fullscreen mode Exit fullscreen mode

Output:

e want t
Enter fullscreen mode Exit fullscreen mode

In this example,the colon used in this slicing operator will return the characters from index 1 upto index 8(so not including index 9) and the increment will be 1.

String Operators

Concatenation

We can concatenate strings by using the plus(+) sign.

var1 = "we want"
var2 = "to visit a"
var3 = "zoo"
var =  var1 +" "+ var2 +" "+ var3             #concetenation of a string
print(var)
Enter fullscreen mode Exit fullscreen mode

Output:

we want to visit a zoo
Enter fullscreen mode Exit fullscreen mode

Notice one thing, this + sign doesn't add any space while concatenating.

Repetition

We can create a new string with the specified number of copies of the input string using this method.

v = "repeat4time"*4
print(v)
Enter fullscreen mode Exit fullscreen mode

Output:

repeat4timerepeat4timerepeat4timerepeat4time
Enter fullscreen mode Exit fullscreen mode

Built-in methods

It's important to remember that, Python is IMMUTABLE. Immutable means once it has been created its value cannot be changed.
So, each time we have to modify the values, we need to make a copy of the original one and make changes to the duplicate one.

me = "Abira"
me[1] = "e"
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError: 'str' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode

Python has some built-in method to access or process characters in string.

For example,

count(substring) method

we can use the count method to count the occurances of a particular substring.

place = "I want to visit USA"
print(place.count("i"))
Enter fullscreen mode Exit fullscreen mode

Output:

2
Enter fullscreen mode Exit fullscreen mode

As python is case-sensitive, we can't access I here cause the ASCII value of I is different than i***

index(substring) method

we can use the index method to find the index of the first occurance of a given substring.

place = "I want to visit USA"
print(place.index("i"))
Enter fullscreen mode Exit fullscreen mode

Output:

11
Enter fullscreen mode Exit fullscreen mode

Upper() and lower() method

Upper returns the **copy* of a given string in all uppercase letters; while lower returns the copy of a given string in all lowercase letters.

place = "I want to visit USA"
print(place.upper())
print(place.lower())
Enter fullscreen mode Exit fullscreen mode

Output:

I WANT TO VISIT USA
i want to visit usa
Enter fullscreen mode Exit fullscreen mode

upper or lower method takes no arguments.

strip() method

this strip method returns the copy of a string by removing the whitespaces from before and after letters.

Whitespaces refers to any character that represents a space in text like a tab,a space or a new line character.

new = "   Well this is another line   !     "       #Strips all whitespace characters from both ends.
print(new.strip())
Enter fullscreen mode Exit fullscreen mode

Output:

Well this is another line   !
Enter fullscreen mode Exit fullscreen mode

Notice, the whitespace between characters are not removed, only the before and after letters whitespaces are removed.

replace(oldstring, newstring) method

the replace method replace every instance of oldstring with newstring in a string.

exm = "wd ard hdrd"
nexm = exm.replace('d','e')
print(nexm)
Enter fullscreen mode Exit fullscreen mode

Output:

we are here
Enter fullscreen mode Exit fullscreen mode

split method

Split helps us breaking sentences of a string into more managable pieces.

Split takes a delimiter and splits the string into sub-strings.The method returns a list where each item is a sub-string that is cut at every instance of that delimeter.

For example,

song = "Tell me why? Aint noting but a heartache. Tell me why? Aint noting but a mistake"
print(song.split("?"))
Enter fullscreen mode Exit fullscreen mode

Output:

['Tell me why', ' Aint noting but a heartache. Tell me why', ' Aint noting but a mistake']
Enter fullscreen mode Exit fullscreen mode

This output comes as a list
Here "?" is the delimeter.. so It will cut in those places and won't return the delimeter in output.

x = "Library is a place where you can find peace"
print(x.split(" "))
Enter fullscreen mode Exit fullscreen mode

Output:

['Library', 'is', 'a', 'place', 'where', 'you', 'can', 'find', 'peace']
Enter fullscreen mode Exit fullscreen mode

Here my delimeter is a space. So the resulting list will include every word in that sentence but no spaces.

x = "Library is a place where you can find peace"
print(x.split("a"))
Enter fullscreen mode Exit fullscreen mode

Output:

['Libr', 'ry is ', ' pl', 'ce where you c', 'n find pe', 'ce']
Enter fullscreen mode Exit fullscreen mode

So, the split method won't include the delimeter in the list it returns.

Join method

The inverse of the split method is join. We can choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.

x = ["*light blue?", "sky", "it's raining hard","colin, where you go","?*"]
y = "! "
p = y.join(x)
print(p)
Enter fullscreen mode Exit fullscreen mode

Output:

*light blue?! sky! it's raining hard! colin, where you go! ?*
Enter fullscreen mode Exit fullscreen mode

We can also use empty string or multi-character strings as glue.

Discussion (5)

Collapse
cgifl300 profile image
cGIfl300

python.org/dev/peps/pep-0257/ :-) """blah""" is just a string as a normal "string" or 'string'.

You also forgot the f-string.

a = "Hello"
print(f"{a} World!")
Enter fullscreen mode Exit fullscreen mode
Collapse
mouly22 profile image
Umme Abira Azmary Author

Hey! It just slipped my mind.. Thank you @cgifl300 😊

Collapse
mouly22 profile image
Umme Abira Azmary Author

As we know """string""" with triple quotations are normal '''string''' but we can also add multiple lines using it; that's why I just mentioned it.. well I will clarify it! Thank you @cgifl300

Collapse
ridima_zahan_345 profile image
Ridima Zahan

nice 😊

Collapse
mouly22 profile image
Umme Abira Azmary Author

Hope it may help you 😊