• Strings in python are surrounded by either single
quotation marks, or double quotation marks.
• 'hello' is the same as "hello".
print("Hello")
print('Hello')
a = "Hello"
print(a)
• a = ""“Amit kumar
Nandanwar cse
Dept manit bhopal"""
print(a)
• Try it Yourself »
• a = ‘‘’ I
Love
India.'''
• print(a)
• Python does not have a character data type, a single
character is simply a string with a length of 1.
• Square brackets can be used to access elements of the
string.
•
a = "Hello, Amit!"
print(a[3])
• a = "Hello, Amit!"
print(len(a))
Slicing
• You can return a range of characters by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
• b = "Hello, World!"
print(b[2:6])
• Get the characters from position 2 to position 6 (not
included):
• a = "Hello, World!"
print(b[:4])
• print(b[2:])
• b = "Hello, World!"
print(b[-5:-2])
• Try it Yourself »
Built-in methods on strings.
• Upper Case
• a = "Hello, World!"
print([Link]())
Lower Case
• a = "Hello, World!"
print([Link]())urself »
• Remove Whitespace
• Whitespace is the space before and/or after the actual text, and very
often you want to remove this space.
• a = " Hello, World! "
print([Link]())
• Replace String
• a = "Hello, World!"
print([Link]("H", "J"))urself
Split String
• The split() method returns a list where the text between the specified
separator becomes the list items.
• a = "Hello, World!"
• b = [Link]("r")
• print(b)
String Concatenation
• To concatenate, or combine, two strings you can use the +
operator.
• a = "Hello"
b = "World"
c = a + b
print(c)
• Try it Yourself »
• a = "Hello"
b = "World"
c = a + " " + b
print(c)