Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python String(85mn)

Uploaded by

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

Python String(85mn)

Uploaded by

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

វិទ្យាល័យព្រះសុស ុ ត្ថិ-សាលារៀនជំនានជថ្៊ីម

៊ី វ

មុខវិជ្ជា ៖ រ័ត្៌មាជវិទ្យា

ថ្នាកថទ្យ៊ី ៖ 10NGS-C1

រមរៀនជទ្យ៊ី៩ ៖ Python String

បរព្រនជរោយ ៖ រ ះ លកខិណ

ឆ្ា នសិកា ៖ ២០២៤-២០២៥


វត្ថបន
ុ ណរនជរមរៀនជ

- អាចកំណត់បានពកា
ី របង្កត
ី ន្
ិ ង្ប្ី បាស់ Python String

- អាចង្វកា
ី រ modify strings

- អាចង្វកា
ី រ slicing string
ៀ នឭករមរៀនជចាសថ

- ដូចងដេចដដលងៅថា Number data type?

- ងតី int ន្
ិ float មានលកខណៈដូចគ្នា ន្
ិ ខុសគ្នាអ្វខ
ី ះល ?

- ដូចងដេចដដលងៅថា User input? ចូរងលក


ី ឧទាហរណ៍បញ្ជាក់។
ខលឹមសាៀ

▪ Introduce to string
▪ String Creation
▪ Accessing character
▪ String Slicing
▪ Deleting String
▪ String modify
▪ Concatenating and Repeating
▪ Format String
Introduce to String
▪ A string is a sequence of characters.
▪ Characters can include letters, numbers,
symbols, and whitespace.
▪ Strings are immutable,
▪ Strings enclosed within either single
quotes (' '), double quotes (" "), or triple
quotes (''' ''' or """ """).
String Creation

▪ Python offers multiple ways to define them, catering to different use


cases such as single-line, multi-line, and strings containing quotes.
▪ Strings can be created using either single (‘) or double (“) quotes.
▪ Multi-line Strings : If we need a string to span multiple lines then we
can use triple quotes (”’ or “””).
Example:
Accessing Character

▪ Strings in Python are sequences of


characters, so we can access
individual characters using indexing.
▪ Strings are indexed starting
from 0 and -1 from end.
Accessing Character
▪ Access string with index
Note : Accessing an index out of
range will cause an IndexError.
Only integers are allowed as
indices and using a float or other ▪ Access string with negative index
types will result in a TypeError.
String Slicing

▪ Slicing is a way to extract portion of a string by specifying


the start and end indexes.
▪ Syntax string[start:end], where start starting index
and end is stopping index (excluded).
String Immutability

▪ Strings in Python are immutable.


▪ Cannot be changed after created.
▪ To manipulate strings can use
methods like concatenation,
slicing, or formatting to create new
strings based on the original.
Deleting a String

▪ It is not possible to delete individual characters from a string.

▪ We can delete an entire string variable using the del keyword.

▪ Note: After deleting the string using del and if we try to


access s then it will result in a NameError because the
variable no longer exists.
Concatenating and Repeating Strings

▪ Concatenating is combining two or more strings together .you


can use the + operator.
▪ We can repeat a string multiple times using * operator.
Updating a String

▪ To update a part of a string we need to create a new string


▪ repalce() method use to replaces a specified phrase with
another specified phrase.
synctax : string.replace(oldvalue, newvalue, count)
Common String Methods

▪ len() function returns the total number of characters in a string.


▪ upper() method converts all characters to uppercase.
▪ lower() method converts all characters to lowercase.
▪ strip() removes leading and trailing whitespace from the string
▪ replace()replaces a specified phrase with another specified
phrase.
Format Strings

▪ Format strings are a way to insert variables into strings by using


f-string or format() method.
▪ curly brackets {} are placeholders for variables and other operations.
ហវក
ឹ ហាត់១

▪ How do you create a string variable in Python?

A) string = 'Hello’
B) string = "Hello“
C) Both A and B
D) Neither A nor B
ហវក
ឹ ហាត់២

▪ What will the following code output?

A) H
B) e
C) !
D) l
ហវក
ឹ ហាត់៣

▪ Which method can be used to remove any whitespace from


the beginning or the end of a string?

A) strip()
B) trim()
C) remove()
D) delete()
ហវក
ឹ ហាត់៤

▪ How do you concatenate two strings in Python?

A) Using the + operator


B) Using the & operator
C) Using the concat() function
D) Using the append() method
ហវក
ឹ ហាត់៥

▪ Which method can be used to replace parts of a string?

A) replace()
B) switch()
C) substitute()
D) change()
ហវក
ឹ ហាត់៦

▪ Which of the following is a valid way to format a string using


f-strings?

A) f”Hello {name}“
B) f’Hello {name}’
C) f”””Hello {name}”””
D) All answers are correct
ហវក
ឹ ហាត់៧

▪ What does the len() function do when applied to a string?

A) Returns the number of characters in the string


B) Returns the last character of the string
C) Returns the first character of the string
D) Returns the number of words in the string

You might also like