100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Python Strings Tutorial: Methods, Examples & Best Practices for Beginners

Rating
-
Sold
-
Pages
63
Uploaded on
23-10-2025
Written in
2025/2026

Python Strings Tutorial: Methods, Examples & Best Practices for Beginners












Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
October 23, 2025
Number of pages
63
Written in
2025/2026
Type
Summary

Subjects

Content preview

Python Strings

6.1 Introduction to python strings
6.1.1 String Indexing
6.1.2 String slicing
6.1.3 Quotes and Apostrophes
6.1.4 Formatting in strings
6.1.5 Truth value testing of String
6.1.6 Escape Sequences

6.2 String Operations
6.2.1 Concatenation ‘+’ operator
6.2.2 Replication ‘*’ operator
6.2.3 Membership ‘in’ operator
6.3 String Methods
6.3.1 len()
6.3.2 capitalize()
6.3.3 count()
6.3.4 lower()
6.3.5 upper()
6.3.6 find()
6.3.7 isalnum()
6.3.8 isalpha()
6.3.9 isdigit()
6.3.10 islower()
6.3.11 isupper()
6.3.12 lstrip()
6.3.13 rstrip()
6.3.14 isspace()
6.3.15 istitle()
6.3.16 replace()
6.3.17 join()
6.3.18 swapcase()
6.3.19 partition()
6.3.20 split()
6.3.21 index()
6.3.22 max() and min() method
6.4 Introduction to Python module
6.5 Math Module
6.6 Random Module
6.7 Statistics Module
6.7.1 Mean()
6.7.2 Median
6.7.3 Mode()

,6.1 Python Strings:
A string is a sequence of characters like your name, country, address , city ,
subject , all are examples of a string. In this Chapter you will see how strings are
treated in Python, the different ways in which strings are represented, various string
operations and methods which can be used with strings in your code.

Strings in Python are identified as a contiguous set of characters enclosed in
quotation marks. Python allows the use of quotation marks to indicate string literals. To
create a string, put the sequence of characters inside either single quotes, double quotes,
or triple quotes and then assign it to a variable You can use any one , single, double, or
triple quotes but you must start and end the string with the same type. You would use
the triple quotes when your string runs across several lines. Let us see some examples
of creating strings.

S= ‚Hello World‛
S1=‛123456‛
S2=’Save Earth‛
S3=‛‛‛Introduction
to Python Strings‛‛‛
In the examples above, Please note that all the variables S,S1,S2,S3 that have been
initiated with single, double, or triple quotes are taken as string.

Remember: You can use single and double quotes for a single line of characters.
Multiple lines are generally put in triple quotes.

Example 6.1 :What is the output of the following statements :
print(1+2)
print(‚1‛+‛2‛)

output:
3
12
The first line is a result of adding two numbers. The second line is a result
of concatenating two strings.


Let us see what happens when you do not include any quotes:

>>>print (Hello)

,NameError: name 'Hello' is not defined



Without the quotes, Python reads Hello as a variable that is undefined:
As we saw above, Python will not print an undefined variable, that is why we get the name error.


All Python strings has the following features:
● An individual character in a string is accessed using a subscript (index). The
subscript should always be an integer (positive or negative) and starts from 0.
● Strings are immutable i.e. the contents of the string cannot be changed after it is
created. Once a string's value is established, it can't be changed without
creating a new variable. For example : This statement will raise an error
if you try to change the content of the string :

word = "Bollywood‛
word*0+ = "H‛
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
word[0]='h'
TypeError: 'word ' object does not support item assignment
In order to make changes to a String you will need to create a new String variable .This
new variable can then hold the changed version of the String.
word=‛Hollywood‛ will create a new version of the string word.
● We can use the operators like + (Concatenation) , * (Repetition ) ,% (formatting) ,
in (Membership) ,not in , range (start, stop[,step]) with the strings .


6.1.1 String Indexing


As, string is a sequence, its elements can be accessed through indexing. Indexing strings
is the most useful, when the string elements need to be accessed. Use the square
brackets [] to specify which part of the string you want to select.

Def: Indexing is a way to reference individual elements within a string based on their position

Each of a string’s characters correspond to an index number, starting with the index
number 0. For the string ‚SAVE WATER ‚ the index breakdown looks like this:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1

, S A V E W A T E R

0 1 2 3 4 5 6 7 8 9



For example, if we have the string
str= ‚SAVE WATER ‚
When we refer to a particular index number of a string, Python returns the character that is in
that position. If we want to display the character in the zero position (that is, the first
character we can write :
>>> print (str[0])

The positions in a string are numbered starting with 0, so this evaluates to 'S', other
values can be displayed as :
So
str[1] is ‘A’
str[3] is ‘E’
str[9] is ‘R’

Remember : Indexes always start with zero. The first character in a string is considered the ‚0th‛ element
in the string. The index of last character in a string is equal to the length of the string – 1.


We also notice that the whitespace character between Save and Water also corresponds
with its own index number. In this case, the index number associated with the
whitespace is 4.

Any other symbol or punctuation mark, such as !,*,#,$,&,.,;,?, is also a character and
would be associated with its own index number if present in the string .

The fact that each character in a Python string has a corresponding index number
allows us to access and manipulate strings in the same ways ,as we can with other
sequential data types.

We can use negative integers to identify character positions relative to the end of the
string. Negative indexes which count backwards from the end of the string, starting at
the index number -1 as shown in the above figure. :
str[-1] is ‘R’
str[-4] is ‘A’
str[-8] is ‘V’
$17.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
buildersnewest

Get to know the seller

Seller avatar
buildersnewest Auburn University At Montgomery
View profile
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
4 months
Number of followers
0
Documents
14
Last sold
1 month ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions