Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Samenvatting

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

Beoordeling
-
Verkocht
-
Pagina's
63
Geüpload op
23-10-2025
Geschreven in
2025/2026

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

Instelling
Vak

Voorbeeld van de inhoud

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’

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
23 oktober 2025
Aantal pagina's
63
Geschreven in
2025/2026
Type
SAMENVATTING

Onderwerpen

€16,48
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper
Seller avatar
buildersnewest

Maak kennis met de verkoper

Seller avatar
buildersnewest Auburn University At Montgomery
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
1
Lid sinds
8 maanden
Aantal volgers
0
Documenten
13
Laatst verkocht
5 maanden geleden

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen