Strings
STRING
Strings are sequences of characters. There are numerous algorithms for processing strings,
including for searching, sorting, comparing and transforming. Python strings are "immutable"
which means they cannot be changed after they are created .
Strings are arrays of bytes representing Unicode characters.
print("Hello")
print('Hello')
Assign String to a Variable
a = "Hello"
print(a)
Access characters in a string
In order to access characters from String, use the square brackets [] for slicing along with
the index or indices to obtain your characters. Python String index starts from 0.
a = "Hello, World!“
print(a[1])
◻ Python allows negative indexing for its sequences.
◻ The index of -1 refers to the last item, -2 to the second last item and so on.
print(a[-1])
String Length
To get the length of a string, use the len() function.
a = "Hello, World!"
print(len(a))
Check String
txt = "The best things in life are free!"
print("free" in txt)
txt = "The best things in life are free!"
print(“if" in txt)
txt = "The best things in life are free!"
print(“in" in txt)
txt = "The best things in life are free!"
print("for" in txt)
“in” keyword can also be used with if
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
Check if NOT
1) txt = "The best things in life are free!"
print("expensive" not in txt)
2) txt = "The best things in life are free!"
print(“good" not in txt)
3) txt = "The best things in life are free!"
print(“things" not in txt)
4) txt = "The best things in life are free!"
print(“if" not in txt)
“not in” keyword can also be used with if
◻ txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
Slicing
◻ b = "Hello, World!"
print(b[2:5])
◻ b = "Hello, World!"
print(b[:5])
◻ b = "Hello, World!"
print(b[2:])
◻ b = "Hello, World!"
print(b[5:-2])
Slicing
◻ b = "Hello, World!"
print(b[:-1])
◻ b = "Hello, World!"
print(b[5:-2])
Modify Strings
◻ Python has a set of built-in methods that you can use on strings.
◻ upper(), lower(), split(), strip(), replace()
◻ a = "Hello, World!"
print([Link]())
◻ a = "Hello, World!"
print([Link]())
◻ a = " Hello, World! "
print([Link]())
◻ a = "Hello, World!"
print([Link]("H", "J"))
◻ a = "Hello, World!"
print([Link](",")) # returns ['Hello', ' World!']
Splitting
◻ word = 'CatBatSatFatOr'
◻ print([Link]('t'))
String Concatenation
◻ To concatenate, or combine, two strings you can use the + operator.
◻ a = "Hello"
b = "World"
c=a+b
print(c)
◻ a = "Hello"
b = "World"
c=a+""+b
print(c)
Use the len method to print the length of the string.
◻ x = "Hello World"
◻ print( --------- )
Get the first character of the string txt.
◻ txt = "Hello World"
◻ x = …………..
Get the characters from index 2 to index 4 (llo).
◻ txt = "Hello World"
◻ x = …………..
Return the string without any whitespace at the beginning
or the end.
◻ txt = " Hello World "
◻ x = ……………
Convert the value of txt to upper case.
◻ txt = "Hello World"
◻ txt =
Replace the character H with a J.
◻ txt = "Hello World"
◻ txt = txt.(….. , …….)
Insert the correct syntax to add a placeholder for the age
parameter
◻ age = 36
◻ txt = "My name is John, and I am ……."
◻ print([Link](age))
Summary
String
Operation on String.
Thank You