- A single character is also considered a string
- Strings are immutable in nature
- Square brackets are used to access elements of strings
- In python strings are surrounded by a single or double quotation marks (‘Hey’ is same as “Hey” )
STRING MANIPULATION
In this article you would learn basics of string manipulation with Python. To understand it better lets have a look at the table below:
|
STRING |
P |
Y |
T |
H |
O |
N |
|
POSITION |
0 |
1 |
2 |
3 |
4 |
5 |
As shown in the table, our string is “PYTHON” and each of the character is positioned from 0 to 5 (note the numbering starts from zero). Now we can do lot of things with this string using python code. Here is some of it for you to get going, have a look. Each line of code is followed by a # or comment explaining what each line of code does.
x='PYTHON' #assign the string “Python” to variable x
length_of_string = len(x) #gives the length of the string
print ('No of characters in string PYTHON are:')
print (length_of_string) # print the value of length_of_string
#string slicing: take part of string out
print (x[1:4]) # starting from 1st till 4th but excluding 4th
print (x[2:]) # starting from 2nd till end
print (x[:4]) # starting from start till 4th but excluding 4th
print (x+ " is awesome !") # string concatenation
Assignment Operator : Assignment operator is used to assign the strings to a variable.
name="shyam"
Concatenation Operator : To concatenate two strings we use ‘+’ operator.
greet1="Hello“ greet2="World“ greetings = greet1 + greet2
Output: ‘HelloWorld’
Escape Sequence Operator : Used to insert non allowed characters in between the strings. Example of non-allowed characters can be the use of double quotes ‘ “ ‘ inside a string assigned with double quotes. Escape sequence operator is usually written using a backslash ‘’ followed by non allowed characters.
x = "Learn datascience from "Klaymatrix Data Labs" easily"
If we print state, we would get an error. So we need to re-write it as:
x = “Learn datascience from “Klaymatrix Data Labs” easily”
Now the output would be:
Learn datascience from “Klaymatrix Data Labs” easily
String repetition operator : ‘*’ operator is used to repeat the string or substring within a string.
fruit = 'Ba'+'na’*2
print(fruit)
output: ‘Banana’
Membership operator : Membership operator is used to check if a string is a substring to another.
'na' in 'banana’
output: True
Identity operator: Identity operator is used to check if two identities are identical to each other.
'U' is 'you’
Output: False
Comparison Operator:
‘==’ returns Boolean True if two strings are same and returns Boolean False if the strings are not same.
‘ != ‘ : returns Boolean False if two strings are same and returns Boolean True if the strings are not same.
Note : – An empty string has Boolean value of False.
'Hello' == 'Hii'
Output: False
Important string methods
x=' klaymatrix data labs '
x.upper()
x.lower()
x.strip()#removing whitespaces
x.replace(' ','_')
x.split(" ")
Concatenation
f_name = "KLAYMATRIX" m_name = "DATA" s_name = "LABS" full_name = f_name + " " + m_name + " " + s_name print(full_name)
String formatting
a. Using f-string.
b. Using format() method.
c. Using % operator
xyz=80
#a. f-string
print("the name is " + xyz)
print("the student " + xyz + " got 100% marks")
print(f"the student got {xyz}% marks")
#b. format()
print("{a} is a student".format(a='xyz'))
#c. % operator
a='cats'
b='dogs'
c='human'
print("I like %s , %s and %s at home" %(a,b,c))