0% found this document useful (0 votes)
8 views7 pages

Python String Basics and Methods

The document provides an overview of strings in Python, including how to define them, use multi-line strings, and perform slicing. It also covers built-in string methods, string formatting with f-strings, and escape characters. Additionally, it explains various string methods such as count, find, and isalnum, along with examples.

Uploaded by

Abhishek Kashyap
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)
8 views7 pages

Python String Basics and Methods

The document provides an overview of strings in Python, including how to define them, use multi-line strings, and perform slicing. It also covers built-in string methods, string formatting with f-strings, and escape characters. Additionally, it explains various string methods such as count, find, and isalnum, along with examples.

Uploaded by

Abhishek Kashyap
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

Python String

String
Strings in python are surrounded by either single quotation marks, or double
quotation marks.
'Ximat' is the same as "Ximat".
Example:
print("Ximat")

print('Ximat')

print("My father's name is ....")

print('My father"s name is ....')

Output:

Multline String
Example:
content="hello Ximat"
print(content)
# use of multiline string to a variable by using three quotes:
content='''At Ximat, our mission is to inspire and equip the next generation of
animators and multimedia artists with the skills, knowledge, and confidence to
excel in the global digital media industry. We strive to create a learning
environment that encourages creativity, critical thinking, and technical
proficiency.'''
cont="""We envision a future where our graduates are recognized as leaders
and innovators in the animation and multimedia industries, contributing to
groundbreaking projects and pushing the boundaries of visual storytelling."""
print(content)
print("\n")
print(cont)

1 Contact: 8902755533
Python String

Output:

Slicing
Python string slicing lets you extract parts of a string using the syntax:
string[start:stop:step]
Example:
# print from start upto 4th character
# string[:stop]
cont="Xzen Institute of Media and Technology"
print(cont[:5]) # only display 5-1=4 upto 4th character
Output:

Example:
# string[start:stop]
cont="Xzen Institute of Media and Technology"
print(cont[5:15]) #only display 15-1=14 upto 14th character
Output:

Note: The first character has index 0.

2 Contact: 8902755533
Python String

Python built-in methods / function:


Example:
# using method upper() and lower()
cont="Xzen Institute of Media and Technology"
print([Link]()) # convert to upper case
print([Link]()) # convert to lower case
Output:

Example:
# strip() method removes any whitespace from the beginning or the end:
cont=" CodenTricks "
print(cont)
print("\n")
print([Link]())
Output:

To concatenate, or combine, two strings you can use the + operator.


# concatenate (combine) 3 variable a,b and c
a="Kanchrapara's Best"
b="Institute is"
c="Ximat"
cont=a+" "+b+" "+c
print(cont)

3 Contact: 8902755533
Python String
Output:

String Format:
When comabine / concatate String with other data types like number .... then it will generate
error.

To overcome this issue we will use f-string:

Example:

# concatenate (combine) different data type with fstring

a="Kanchrapara's no." #string

b=1 #number

c="institute Ximat"

cont=f"Welcome to {a} {b} {c}"

print(cont)

Output:

# fstring using placeholder {}

price=59

cont=f"The price AMD RYZEN 5 3400G is {price:.1f} rupee"

print(cont)

Output:

4 Contact: 8902755533
Python String

# Perform a math operation in the placeholder

cont=f"4X6 is {4*6}"

print(cont)

Output:

4X6 is 24

Escape Character:

To insert characters that are illegal in a string, use an escape character. An escape character is
a backslash \ followed by the character you want to insert.

We will use escape character \


# Escape Character for single character \'s

cont='Xzen is Ximat\'s parent company'

print(cont)

# for multi character \" \"

text = "We are the so-called \"Bhartiya\" also."

print(text)

# print \ we will \\

text = "This is \\"

print(text)

# Carriage Return will print after \r part

text = "Hello \rCodentricks ..."

print(text)

Output:

# capitalize() method

content="converts the first character to upper case"

print([Link]())

Output:

Converts the first character to upper case

5 Contact: 8902755533
Python String

# casefold() convert to lowercase

content="Here are the Top 20 phones that you searched for the most in the last 12 months –
we’re including phones that launched in November 2024"

print([Link]())

Output:

here are the top 20 phones that you searched for the most in the last 12 months – we’re
including phones that launched in november 2024

# count() Returns the number of times a specified Character occurs in a string

content="Here are the Top 20 phones that you searched for the most in the last 12 months –
we’re including phones that launched in November 2024"

print([Link]("in"))

Output: 4

# endwith() check last charcter and return True or False

content="Here are the Top 20 phones that you searched for the most in the last 12 months –
we’re including phones that launched in November 2024"

print([Link]("4")) # return True

print([Link]("2")) #return False

# The find() method finds the first occurrence of the specified value, position

content="Here are the Top 20 phones that you searched for the most in the last 12 months –
we’re including phones that launched in November 2024"

print([Link]("12")) # return 70

print([Link]("Ximat")) # return -1 (not existed)

Note: 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.

Example:

content="Here are the Top 20 phones that you searched for the most in the last 12 months –
we’re including phones that launched in November 2024"

print([Link]("12")) # return 70

print([Link]("Ximat")) # return error

6 Contact: 8902755533
Python String

# isalnum() Method heck if all the characters, not alphanumeric: (space)!#%&?

content="FESWTGEW%2423" #have special character %

content1="dshgrh25432dgdsh"

res=[Link]()

res1=[Link]()

print(res) #return False

print(res1) #return True

Similarly isalpha() method only allow alphabets.

7 Contact: 8902755533

You might also like