0% found this document useful (0 votes)
7 views16 pages

String Methods

The document provides an overview of Python string methods, which are built-in functions for creating and manipulating strings without altering the original string. It explains the characteristics of strings in Python, including their creation and various methods like capitalize(), islower(), endswith(), and more, each with syntax and examples. These methods serve various purposes such as formatting, searching, and validating text.

Uploaded by

patil.arun8217
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)
7 views16 pages

String Methods

The document provides an overview of Python string methods, which are built-in functions for creating and manipulating strings without altering the original string. It explains the characteristics of strings in Python, including their creation and various methods like capitalize(), islower(), endswith(), and more, each with syntax and examples. These methods serve various purposes such as formatting, searching, and validating text.

Uploaded by

patil.arun8217
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

STRING METHODS:

Python string methods are built-in functions used to efficiently create new strings or return
information about an existing string without modifying the original (as strings are
immutable). They offer functionality for tasks such as formatting, searching, splitting, and
validating text.
STRING:

In Python, a string is a sequence of characters written inside quotes. It can include letters, numbers, symbols, and
spaces.

Strings can be created using either single ('...') or double ("...") quotes. Both behave the same.

Multiline strings are created using triple(‘’’….’’’)quotes.

Example:

# Creating strings

single_quote_string = 'Hello, World!‘

double_quote_string = "Python Programming"

multiline_string = """This is a string that spans multiple lines.""“

# Printing strings

print(single_quote_string)

print(double_quote_string)

print(multiline_string)
STRING METHODS:
[Link]():

The capitalize() method returns a string where the first character is upper case, and the rest is lower case.

Syntax

[Link]()

Example:

txt = "hello, and welcome to my world."


x = [Link]()
print (x)

[Link]();

The islower() method returns True if all the characters are in lower case, otherwise False.

Numbers, symbols and spaces are not checked, only alphabet characters.

Syntax

[Link]()

Example:

txt = "hello world!"


x = [Link]()
print(x)
[Link]():

The endswith() method returns True if the string ends with the specified value, otherwise False.

Syntax

[Link](value, start, end)

Example:

txt = "Hello, welcome to my world."


x = [Link](".")
print(x)

Parameter Description

value Required. The value to check if the string ends with. This value parameter can also
be a tuple, then the method returns true if the string ends with any of the tuple
values.

start Optional. An Integer specifying at which position to start the search

end Optional. An Integer specifying at which position to end the search


[Link]():

The startswith() method returns True if the string starts with the specified value, otherwise False.

Syntax

[Link](value, start, end)

Example:

txt = "Hello, welcome to my world."


x = [Link]("Hello")
print(x)

Parameter Description

value Required. The value to check if the string starts with. This value parameter can
also be a tuple, then the method returns true if the string starts with any of the
tuple values.

start Optional. An Integer specifying at which position to start the search

end Optional. An Integer specifying at which position to end the search


[Link]():

The isupper() method returns True if all the characters are in upper case, otherwise False.

Numbers, symbols and spaces are not checked, only alphabet characters.

Syntax

[Link]()

Example:

txt = "THIS IS NOW!"


x = [Link]()
print(x)

[Link]():

The isdigit() method returns True if all the characters are digits, otherwise False.

Exponents, like ², are also considered to be a digit.

Syntax

[Link]()

Example:

txt = "50800"
x = [Link]()
print(x)
[Link]():

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.

Exponents, like ² and ¾ are also considered to be numeric values.

"-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the -
and the . are not.

Syntax

[Link]()

Example:

txt = "565543"
x = [Link]()
print(x)

[Link]():

The upper () method returns a string where all characters are upper case.

Symbols and Numbers are ignored.

Syntax

[Link]()

Example:

txt = "Hello my Friends"


x = [Link]()
print(x)
[Link]():

The title() method returns a string where the first character in every word is upper case. Like a header, or a
title.

If the word contains a number or a symbol, the first letter after that will be converted to upper case.

Syntax

[Link]()

Example:

txt = "Welcome to my world"


x = [Link]()
print(x)

[Link]()

The strip() method removes any leading, and trailing whitespaces.

Leading means at the beginning of the string, trailing means at the end.

You can specify which character(s) to remove, if not, any whitespaces will be removed.

Syntax

[Link](characters)

Parameter Description

characters Optional. A set of characters to remove as leading/trailing characters


Example:

txt = " banana "


x = [Link]()
print("of all fruits", x, "is my favorite")

[Link]():

The replace() method replaces a specified phrase with another specified phrase.

Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

Syntax

[Link](oldvalue, newvalue, count)

Parameter Description

oldvalue Required. The string to search for

newvalue Required. The string to replace the old value with

count Optional. A number specifying how many occurrences of the old value you want to
replace. Default is all occurrences
Example:

txt = "I like bananas"


x = [Link]("bananas", "apples")
print(x)

[Link]():

The join() method takes all items in an iterable and joins them into one string.

A string must be specified as the separator.

Syntax

[Link](iterable)

Example:

myTuple = ("John", "Peter", "Vicky")


x = "#".join(myTuple)
print(x)

Parameter Description
iterable Required. Any iterable object where all the returned values are
strings
[Link]():

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax

[Link](separator, maxsplit)

Example:

txt = "welcome to the jungle"


x = [Link]()
print(x)

Parameter Description

separator Optional. Specifies the separator to use when splitting the string. By default any
whitespace is a separator

maxsplit Optional. Specifies how many splits to do. Default value is -1, which is "all
occurrences"
[Link]():

The lower() method returns a string where all characters are lower case.

Symbols and Numbers are ignored.

Syntax

[Link]()

Example:

txt = "Hello my FRIENDS"


x = [Link]()
print(x)

[Link]():

The isalpha() method returns True if all the characters are alphabet letters (a-z).

Example of characters that are not alphabet letters: (space)!#%&? etc.

Syntax

[Link]()

Example:
txt = "CompanyX"
x = [Link]()
print(x)
[Link]():

The find() method finds the first occurrence of the specified value.

The find() method returns -1 if the value is not found.

The find() method is almost the same as the index() method, the only difference is that the index() method
raises an exception if the value is not found. (See example below)

Syntax

[Link](value, start, end)

Example:
txt = "Hello, welcome to my world."
x = [Link]("welcome")
print(x)

Parameter Description

value Required. The value to search for

start Optional. Where to start the search. Default is 0

end Optional. Where to end the search. Default is to the end of the string
[Link]():

The count() method returns the number of times a specified value appears in the string.

Syntax

[Link](value, start, end)

Example:

txt = "apples are sweet, apple are my favorite fruit"


x = [Link]("apple")
print(x)

Parameter Description

value Required. A String. The string to value to search for

start Optional. An Integer. The position to start the search. Default is 0

end Optional. An Integer. The position to end the search. Default is the end of the
string
[Link]():

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and
numbers (0-9).

Example of characters that are not alphanumeric: (space)!#%&? etc.

Syntax

[Link]()

Example:

txt = "Company12"
x = [Link]()
print(x)

[Link]():

The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.

Syntax

[Link]()

Example:

txt = " "


x = [Link]()
print(x)

You might also like