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

Pythongstringfuncs

Uploaded by

ranganadh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Pythongstringfuncs

Uploaded by

ranganadh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

format() Method

----------------
Insert the price inside the placeholder, the price should be in fixed point, two-
decimal format:

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


print(txt.format(price = 49))

The Placeholders
----------------
The placeholders can be identified using named indexes {price}, numbered indexes
{0}, or even empty placeholders {}.

Example
Using different placeholder values:

txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)


txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36)

index() Method
--------------
Where in the text is the word "welcome"?:

txt = "Hello, welcome to my world."


x = txt.index("welcome")
print(x)

Syntax
string.index(value, start, end)

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

Example
Where in the text is the first occurrence of the letter "e"?:

txt = "Hello, welcome to my world."


x = txt.index("e")
print(x)

Example
Where in the text is the first occurrence of the letter "e" when you only search
between position 5 and 10?:

txt = "Hello, welcome to my world."


x = txt.index("e", 5, 10)
print(x)

Example
If the value is not found, the find() method returns -1, but the index() method
will raise an exception:

txt = "Hello, welcome to my world."


print(txt.find("q"))
print(txt.index("q"))

isalnum() Method

Check if all the characters in the text are alphanumeric:

txt = "Company12"
x = txt.isalnum()
print(x)

txt = "Company 12"


x = txt.isalnum()
print(x)

String isalpha() Method

Check if all the characters in the text are letters:

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

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

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

isascii() Method

Check if all the characters in the text are ascii characters:

txt = "Company123"
x = txt.isascii()
print(x)

The isascii() method returns True if all the characters are ascii characters (a-
z).

isdecimal() Method

Check if all the characters in a string are decimals (0-9):

txt = "1234"
x = txt.isdecimal()
print(x)

isdigit() Method

Check if all the characters in the text are digits:

txt = "50800"

x = txt.isdigit()

print(x)

The isdigit() method returns True if all the characters are digits, otherwise
False.
Exponents, like ², are also considered to be a digit.

islower() Method

Check if all the characters in the text are in lower case:

txt = "hello world!"


x = txt.islower()
print(x)

Check if all the characters in the texts are in lower case:

a = "Hello world!"
b = "hello 123"
c = "mynameisPeter"

print(a.islower())
print(b.islower())
print(c.islower())

isspace() Method

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

txt = " s "

x = txt.isspace()

print(x)

isupper() Method

Check if all the characters in the text are in upper case:

txt = "THIS IS NOW!"

x = txt.isupper()

print(x)

join() Method

Join all items in a tuple into a string, using a hash character as separator:

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


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

Join all items in a dictionary into a string, using the word "TEST" as separator:

myDict = {"name": "John", "country": "Norway"}


mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)

ljust() Method

The ljust() method will left align the string, using a specified character (space
is default) as the fill character.

Using the letter "O" as the padding character:

txt = "banana"
x = txt.ljust(20, "O")
print(x)

lower() Method

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

Symbols and Numbers are ignored.

Lower case the string:

txt = "Hello my FRIENDS"

x = txt.lower()

print(x)

partition() Method

ExampleGet your own Python Server


Search for the word "bananas", and return a tuple with three elements:

1 - everything before the "match"


2 - the "match"
3 - everything after the "match"

txt = "I could eat bananas all day"

x = txt.partition("bananas")

print(x)

zfill() Method

The zfill() method adds zeros (0) at the beginning of the string, until it reaches
the specified length.

If the value of the len parameter is less than the length of the string, no filling
is done.

Fill the strings with zeros until they are 10 characters long:

a = "hello"
b = "welcome to the jungle"
c = "10.000"

print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))

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

Symbols and Numbers are ignored.


txt = "Hello my friends"

x = txt.upper()

print(x)

swapcase() Method

The swapcase() method returns a string where all the upper case letters are lower
case and vice versa.

txt = "Hello My Name Is PETER"

x = txt.swapcase()

print(x)

strip() Method
Remove spaces at the beginning and at the end of the string:

txt = " banana "

x = txt.strip()

print("of all fruits", x, "is my favorite")

startswith() Method

Check if the string starts with "Hello":

txt = "Hello, welcome to my world."

x = txt.startswith("Hello")

print(x)

splitlines() Method

The splitlines() method splits a string into a list. The splitting is done at line
breaks.

split the string, but keep the line breaks:

txt = "Thank you for the music\nWelcome to the jungle"

x = txt.splitlines(True)

print(x)

rsplit() Method

Split a string into a list, using comma, followed by a space (, ) as the separator:

txt = "apple, banana, cherry"

x = txt.rsplit(", ")

print(x)
The rsplit() method splits a string into a list, starting from the right.

Split the string into a list with maximum 2 items:

txt = "apple, banana, cherry"

# setting the maxsplit parameter to 1, will return a list with 2 elements!


x = txt.rsplit(", ", 1)

print(x)

You might also like