0% found this document useful (0 votes)
56 views

Python String Methods

This document discusses various string methods in Python including upper() to convert a string to uppercase, lower() to convert to lowercase, split() to split a string into a list of substrings, strip() to remove leading and trailing whitespace, replace() to replace substrings, find() to find the index of a substring, startswith() to check if a string starts with a substring, and endswith() to check if a string ends with a substring. Examples are provided for each method.

Uploaded by

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

Python String Methods

This document discusses various string methods in Python including upper() to convert a string to uppercase, lower() to convert to lowercase, split() to split a string into a list of substrings, strip() to remove leading and trailing whitespace, replace() to replace substrings, find() to find the index of a substring, startswith() to check if a string starts with a substring, and endswith() to check if a string ends with a substring. Examples are provided for each method.

Uploaded by

Mahmoud Elhady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PYTHON

STRING METHODS

Kunal bedre
1
upper()
Converts all characters in a
string to uppercase.
#Example -
s = "hello world"
print(s.upper())

#Output -
HELLO WORLD
2
lower()
Converts all characters in a
string to lowercase.
#Example -
s = "HELLO WORLD"
print(s.lower())

#Output -
hello world
3
split()
Splits a string into a list of
substrings based on a
delimiter. If no delimiter is
specified, whitespace is used
as the default delimiter.
#Example -
s = "hello,world"
print(s.split(","))
#Output -
['hello', 'world']
4
strip()
Removes any leading or trailing
whitespace from a string.
#Example -
s = " hello world "
print(s.strip())
#Output -
hello world
5
replace()
Replaces all occurrences of a
substring with another
substring.

#Example -
s = "hello world"
print(s.replace("world",
"python"))
#Output -
hello python
6
find()
Returns the index of the first
occurrence of a substring in a
string. Returns -1 if the
substring is not found.
#Example -
s = "hello world"
print(s.find("world"))
#Output -
6
7
startswith()
Returns True if a string starts
with a specified substring.
Otherwise, returns False.
#Example -
s = "hello world"
print(s.startswith("hello"))
#Output -
True
8
endswith()
Returns True if a string ends
with a specified substring.
Otherwise, returns False.
#Example -
s = "hello world"
print(s.endswith("world"))
#Output -
True
LIKE
IF YOU FOUND THIS POST

HELPFUL
Kunal bedre

You might also like