Python Strings
Table Of Contents:
- What Is Python String?
- Data Type?
- Multiline Strings
- Array Of Strings
- Looping Through String
- Length Of String
- String Membership Check
- String Slicing
- String Updation
- String Concatenation
- String Repetition
- String Formatting
(1) What Is Python String Data Type?
- The string is a sequence of characters.
- Generally, strings are represented by either single or double quotes in Python.
- A String data type can be of any length. Starting from an empty string to a paragraph.
Examples
str1 = "" #Empty String
str2 = "A" #Single String
str3 = "Hello" #Single Word String
str4 = "Hello World" #Double Word String
str5 = "Hello Programmer, How Are You Doing All?" #A Sentence As A String.
print(str1)
print(str2)
print(str3)
print(str4)
print(str5)
Output:
A
Hello
Hello World
Hello Programmer, How Are You Doing All?
(2) Multiline Strings
- You can assign a multiline string to a variable by using three quotes(”’ ”’).
- Multiline strings can be used for writing paragraphs or comments in your code.
Examples
string = ''' “Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
— Martin Fowler '''
print(string)
Output:
“Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
— Martin Fowler
(3) Array Of Strings
- Strings in Python are arrays of characters.
- A single character is simply a string with a length of 1.
- Square brackets ([ ]) can be used to access elements of the string.
- The index starts from zero(0).
Examples
string1 = "Praudyog"
print(string1[5])
string2 = "Hello, How, Are, You, Doing ?"
print(string2[8])
Output:
y
o
(4) Looping Through A String
- Since strings are an array of characters, we can loop through the characters in a string, with a for loop.
Example-1
string = "Praudyog"
for i in string:
print(i)
Output:
P
r
a
u
d
y
o
g
(5) Length Of A String
- You can use the Python len() method to get the length of a string.
Example-1
string = "Praudyog"
print('Length:',len(string))
Output:
Length: 8
(6) Length Of A String
- You can check whether a particular character or a word is present inside a String or not.
- You can use ‘in’ and ‘not in’ keywords to check the presence of a character or a word.
Example-1
string = "Praudyog"
if 'y' in string:
print('y is present')
else:
print('y is not present')
Output:
y is present
Example-2
string = "Hello, How, Are, You, Doing ?"
if 'world' not in string:
print('world is not present')
else:
print('world is present')
Output:
world is not present
(7) String Slicing
- You can use string slicing to extract parts of the string.
Syntax Of String Slicing
string[start index : end index : steps]
Note:
- Start Index: The starting point of slicing.
- End Index: Ending point of slicing. What ever end index you mentioned, it will always be (end index – 1)
- Step Size: increment between each index for slicing.
Example-1
string = "Praudyog"
print(string[0:4])
Output:
Prau
Note:
- Start Index: 0
- End Index: 4 = (end index – 1) = (4 – 1) = 3
- Step Size: 1 ( Default )
Example-2
string = "Praudyog"
print(string[0:7:2])
Output:
Pado
Note:
- Start Index: 0
- End Index: 7 = (end index – 1) = (7 – 1) = 6
- Step Size: 2 , It will jump 2 steps towards end index.
(8) String Updation
- You can update your string using the + operator.
Example-1
string = 'Hello World!'
print(string)
updated = string[:6] + 'Python'
print(updated)
Output:
Hello World!
Hello Python
(9) String Concatenation
- To concatenate or combine two strings you can use the + operator.
Example-1
string1 = "Hello"
string2 = "Programmer"
combined = string1 +" "+ string2
print(combined)
Output:
Hello Programmer
(10) String Repetation
- You can repeat your string multiple times using the * operator.
Example-1
string = 'Hello '
repeated = string * 5
print(repeated)
Output:
Hello Hello Hello Hello Hello
(10) String Formatting
- String formatting is the process of making your strings look good.
- Using this you can dynamically update your strings based on values passed.
- You can use string format operator % for formatting your string.
Example-1
print("My name is %s and I am learning %s" % ('Subrat', 'Python'))
Output:
My name is Subrat and I am learning Python
Example-2
print('Todays temperature is %d degree Celcious' % (35))
Output:
Todays temperature is 35 Degree Celcious
String Format Characters:
- %c character
- %s string conversion via str() prior to formatting
- %i signed decimal integer
- %d signed decimal integer
- %u unsigned decimal integer
- %o octal integer
- %x hexadecimal integer (lowercase letters)
- %X hexadecimal integer (UPPERcase letters)
- %e exponential notation (with lowercase ‘e’)
- %E exponential notation (with UPPERcase ‘E’)
- %f floating point real number
- %g the shorter of %f and %e
- %G the shorter of %f and %E