Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

الاسبوع الثالث

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Python string

‫ بهذا الملف رح‬, ‫ احنا حكينا عنه قبل هيك ب االسابع الي فاتت‬string ‫موضوع ال‬
‫ناخذ اشياء جديدة‬
1-Multiline Strings:
You can assign a multiline string to a variable by using
three quotes (triple quotes)
Example:

2- 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.

: ً ‫ بنستخدمهم في البايثون المطلوب منا حاليا‬Escape character ‫في عنا اكثر من‬
2.1-new line = = \n

2.2-Tab = = \t

Example:

3- Strings are Arrays

-Like many other popular programming languages, strings in


Python are arrays of bytes representing Unicode characters.

-However, Python does not have a character data type, a single


character is simply a string with a length of 1.

-Square brackets can be used to access elements of the string.

‫ عن طريق ال‬access element ‫ انه بقدر اعمل‬list ‫نفس ما اخذنا ب موضوع ال‬
index

- remember that the first character has the position 0


as element ‫ بتحسبو‬space ‫ ال‬: ً ‫مالحظة مهمة جدا ً جدا‬
H e l l o , W o r l d

index 0 1 2 3 4 5 6 7 8 9 10 11

4-Extended Slicing.
The slice syntax is a handy way to refer to sub-parts of
sequences – typically strings and lists. The slice s[start: end] is
the elements beginning at start and extending up to but not
including end but s[start:end:step] will essentially work as a
step value
Example:
‫نشرح المثال وركزو معاي فيه‬
‫عندي جملة ‪python is cool‬‬
‫‪-‬قبل ما افكر اعمل شي بشوف هو من وين بدو يبلش ووين بده ينتهي وبسجلهم على‬
‫جنب بالمثال هذا بده يبلش من االول لالخر و رح يشمل اخر ‪index‬‬
‫ويمشي خطوتين‬
‫‪-‬رح يطبع ال ‪p = p‬‬
‫ويتخطى عن ال ‪ y‬بيوصل لل ‪ t‬وبيطبعها = ‪t‬‬
‫بيرجع مرة ثانية بيتخطى عن ال ‪ h‬وبيطبع ال ‪o = o‬‬
‫بيرجع يتخطى ال ‪ n‬وبيطبع ال ‪space = space‬‬
‫يتخطى ال ‪ i‬وبطبع ‪s = s‬‬
‫بيرجع يتخطى ‪ space‬وبيطبع ‪c = c‬‬
‫بيتخطى ال ‪ o‬وبيطبع ‪ o‬الثانية = ‪o‬‬
‫واخر شي بتخطى عن اخر ‪ o & l‬وبوقف النه مافي اشي يطبعه‬
‫‪-Strings are immutable‬‬
‫‪strings are immutable, that is you cannot change one once‬‬
‫‪you make it:‬‬
‫‪Example:‬‬
‫"‪str="python‬‬
‫‪str [1] =1‬‬
‫‪print(str) = error‬‬
‫‪-String function:‬‬
‫‪-len () : The len function takes as an argument a string and‬‬
‫‪returns an integer, the length of a string.‬‬

‫‪Example:‬‬
my_str = 'Hello World'
len(my_str) ⇒ 11
length of a string ‫ محسوب في ال‬space ‫ال‬-
-string methods:
-Python has a set of built-in methods that you can use on
strings.
-Note: All string methods returns new values. They do not
change the original string.
Methods:

Method Description

capitalize () Converts the first character to upper case

casefold () Converts string into lower case

center() Returns a centered string

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

encode() Returns an encoded version of the string

endswith() Returns true if the string ends with the specified value

expandtabs() Sets the tab size of the string


find() Searches the string for a specified value and returns the position
of where it was found

format() Formats specified values in a string

format_map() Formats specified values in a string

index() Searches the string for a specified value and returns the position
of where it was found

isalnum() Returns True if all characters in the string are alphanumeric

isalpha() Returns True if all characters in the string are in the alphabet

isdecimal() Returns True if all characters in the string are decimals

isdigit() Returns True if all characters in the string are digits

isidentifier() Returns True if the string is an identifier

islower() Returns True if all characters in the string are lower case

isnumeric() Returns True if all characters in the string are numeric


isprintable() Returns True if all characters in the string are printable

isspace() Returns True if all characters in the string are whitespaces

istitle() Returns True if the string follows the rules of a title

isupper() Returns True if all characters in the string are upper case

join() Joins the elements of an iterable to the end of the string

ljust() Returns a left justified version of the string

lower() Converts a string into lower case

lstrip() Returns a left trim version of the string

maketrans() Returns a translation table to be used in translations

partition() Returns a tuple where the string is parted into three parts

replace() Returns a string where a specified value is replaced with a


specified value
rfind() Searches the string for a specified value and returns the last
position of where it was found

rindex() Searches the string for a specified value and returns the last
position of where it was found

rjust() Returns a right justified version of the string

rpartition() Returns a tuple where the string is parted into three parts

rsplit() Splits the string at the specified separator, and returns a list

rstrip() Returns a right trim version of the string

split() Splits the string at the specified separator, and returns a list

splitlines() Splits the string at line breaks and returns a list

startswith() Returns true if the string starts with the specified value

strip() Returns a trimmed version of the string

swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case

translate() Returns a translated string

upper() Converts a string into upper case

zfill() Fills the string with a specified number of 0 values at the


beginning

-String Format
int & string ‫احنا بنعرف في البايثون انه ما بنقدر نجمع بين‬
Example:

format() method ‫لكن احنا بنقدر نجمع بينهم عن طريق استخدام‬


-The format() method takes the passed arguments, formats them,
and places them in the string where the placeholders {} are:
‫ وبدي اجمع بينهم المكان الي‬string & int ‫يعني انا هسا عندي نفس المثال السابق‬
{ }curly bracket ‫ بتحط‬age ‫بدك تضيف فيه ال‬

The format () method takes unlimited number of arguments, and are


placed into the respective placeholders:

-You can use index numbers {index} to be sure the arguments are
placed in the correct placeholders:
-Formatting Types
Inside the placeholders you can add a formatting type to format
the result:

:< Left aligns the result (within the available space)

:> Right aligns the result (within the available space)

:^ Center aligns the result (within the available space)

:= Places the sign to the left most position

:+ Use a plus sign to indicate if the result is positive or negative

:- Use a minus sign for negative values only

: Use a space to insert an extra space before positive numbers (and a


minus sign before negative numbers)
:, Use a comma as a thousand separator

:_ Use a underscore as a thousand separator

:b Binary format

:c Converts the value into the corresponding unicode character

:d Decimal format

:e Scientific format, with a lower case e

:E Scientific format, with an upper case E

:f Fix point number format

:F Fix point number format, in uppercase format


(show inf and nan as INF and NAN)

:g General format

:G General format (using a upper case E for scientific notations)


:o Octal format

:x Hex format, lower case

:X Hex format, upper case

:n Number format

:% Percentage format

.‫الحمد هلل الذي يقدرنا على فعل كل شيء ويوفقنا بفضله وكرمه إلى ما ينفع الناس‬

You might also like