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
.الحمد هلل الذي يقدرنا على فعل كل شيء ويوفقنا بفضله وكرمه إلى ما ينفع الناس