0% found this document useful (0 votes)
26 views4 pages

Untitled Document

The document describes various string methods in Python. Some of the key methods are: 1. capitalize() returns a string with the first character capitalized and the rest lowercase. 2. casefold() returns a string in lowercase, handling more characters than lower(). 3. center() centers a string within a specified length by adding fill characters (default is space).

Uploaded by

shubhamsona2018
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)
26 views4 pages

Untitled Document

The document describes various string methods in Python. Some of the key methods are: 1. capitalize() returns a string with the first character capitalized and the rest lowercase. 2. casefold() returns a string in lowercase, handling more characters than lower(). 3. center() centers a string within a specified length by adding fill characters (default is space).

Uploaded by

shubhamsona2018
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/ 4

1.

The capitalize() method returns a string where the first character is upper
case, and the rest is lower case.
txt = "python is FUN!"

x = txt.capitalize()

print (x)

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

This method is similar to the lower() method, but the casefold() method is
stronger, more aggressive, meaning that it will convert more characters into
lower case, and will find more matches when comparing two strings and both
are converted using the casefold() method.

txt = "Hello, And Welcome To My World!"

x = txt.casefold()

print(x)

3.The center() method will center align the string, using a specified character
(space is default) as the fill character.
txt = "banana"

x = txt.center(20)

print(x)

4.The count() method returns the number of times a specified value appears
in the string.
txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple")

print(x)

5.The endswith() method returns True if the string ends with the specified
value, otherwise False.
txt = "Hello, welcome to my world."
x = txt.endswith(".")

print(x)

Output:
Ture
6.The expandtabs() method sets the tab size to the specified number of
whitespaces.
txt = "H\te\tl\tl\to"

x = txt.expandtabs(2)

print(x)

Hello

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

txt = "Hello, welcome to my world."

x = txt.find("welcome")

print(x)

Output:
7

8.The format() method formats the specified value(s) and insert them inside
the string's placeholder.

The placeholder is defined using curly brackets: {}. Read more about the
placeholders in the Placeholder section below.
The format() method returns the formatted string.

txt = "For only {price:.2f} dollars!"


print(txt.format(price = 49))

For only 49.00 dollars!

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

The index() method raises an exception if the value is not found.

The index() method is almost the same as the find() method, the only
difference is that the find() method returns -1 if the value is not found. (See
example below)

txt = "Hello, welcome to my world."

x = txt.index("welcome")

print(x)

Output:
7

10.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.

txt = "Company12"

x = txt.isalnum()

print(x)

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

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

txt = "CompanyX"
x = txt.isalpha()

print(x)

12.The isascii() method returns True if all the characters are ascii characters
(a-z).
txt = "Company123"

x = txt.isascii()

print(x)

13.The isdecimal() method returns True if all the characters are decimals
(0-9).

This method can also be used on unicode objects. See example below.

txt = "1234"

x = txt.isdecimal()

print(x)

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

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

txt = "50800"

x = txt.isdigit()

print(x)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy