0% found this document useful (0 votes)
46 views9 pages

Python String Basics and Methods

The document provides an overview of string handling in Python, including string creation with single and double quotes, accessing characters, slicing, and built-in string methods such as upper/lower case conversion, whitespace removal, and string replacement. It also covers string concatenation using the '+' operator and demonstrates various examples for clarity. Overall, it serves as a guide for basic string operations in Python programming.

Uploaded by

abhishekabix08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views9 pages

Python String Basics and Methods

The document provides an overview of string handling in Python, including string creation with single and double quotes, accessing characters, slicing, and built-in string methods such as upper/lower case conversion, whitespace removal, and string replacement. It also covers string concatenation using the '+' operator and demonstrates various examples for clarity. Overall, it serves as a guide for basic string operations in Python programming.

Uploaded by

abhishekabix08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

• 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)

You might also like