String គឺជាឃ្លាប្រយោគទាំងឡាយណាដែលមានន័យសេចក្តីអ្វីម៉្យាង។ ដើម្បីបង្កើតវត្ថុមានប្រភេទជា string យើងត្រូវធ្វើដូចខាងក្រោមនេះ៖
1 2 | sentence = "Programming Python!" print (sentence) |
ដូចនេះយើងឃើញថា ដើម្បីបង្កើតវត្ថុណាមួយមានប្រភេទជា string យើងត្រូវប្រើសញ្ញា " " នៅអមសងខាងឃ្លាប្រយោគណាមួយ។ លើសពីនេះទៀត យើងក៏អាចប្រើសញ្ញា ' ' នេះ នៅក្នុងការបង្កើត string បានដែរ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 | sentence1 = "Programming Python!" sentence2 = 'Python is very popular.' print (sentence1) print (sentence2) |
Python is very popular.
ដោយហតុថា យើងអាចប្រើសញ្ញារហូតដល់ទៅពីរបែប នៅក្នុងការបង្កើត string ដូចនេះយើងអាចប្រើសញ្ញាណាមួយជាតួអក្សរនៅក្នុងឃ្លាប្រយោគ នៅពេលដែលយើងប្រើសញញាមួយទៀតនៅអមសងខាងឃ្លានោះ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 | sentence = 'Programming is "Solving problem" in real world.' print (sentence) |
យ៉ាងណាម៉ិញ នៅក្នុងភាសា Python ដើម្បីបង្កើតអត្ថបទណាមួយ ដែលមានច្រើនបន្ទាត់ យើងត្រូវប្រើប្រាស់សញ្ញា """ """ ឬសញ្ញា ''' ''' នៅអមសងខាងអត្ថបទ។
1 2 3 4 5 6 7 8 | sentence = ''' Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. ''' print (sentence) |
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged.
ចំពោះអក្សរពិសេសមួយចំនួន ដែលមាននៅលើខ្ទង់កំព្យូទ័រ ដូចជា Tab, Backspace, Enter ជាដើម យើងត្រូវប្រើសញ្ញា \ រួមផ្សំជាមួយនឹងអក្សរណាមួយទៀត ដើម្បីបង្កើតអក្សរពិសេសទាំងនោះ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 | sentence = 'Lorem Ipsum is simply \n dummy text of the printing and typesetting industry.' print (sentence) |
dummy text of the printing and typesetting industry.
ក្រៅពីអក្សរ \n នេះ នៅមានអក្សរពិសេសមួយចំនួនទៀត ដែលយើងអាចយកមកប្រើ ដោយសរសេរតាមលំនាំដូចគ្នានេះដែរ។ អក្សរពិសេសទាំងនោះមាន៖
\b Backspace \r Enter \" " \' ' \b សម្លេងជួង
ផ្ទុយមកវិញ បើសិនជាយើងចង់ប្រើប្រាស់សញ្ញា \ នេះជាតួអក្សរធម្មតានៅក្នុងឃ្លាប្រយោគណាមួយ យើងចាំបាច់ត្រូវតែសរសេរអក្សរ R ឬ r នៅខាងមុខឃ្លាប្រយោគនោះ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 | sentence = R 'The important document is in folder c:\n\files' print (sentence) |
នៅក្នុងភាសា Python ដែលហៅថាប្រមាណវិធី concatenation operation គឺជាប្រមាណវិធីទាំងឡាយណា ដែលនៅក្នុងនោះ មានការប្រើប្រាស់ operator + ដើម្បីចំលងយកវត្ថុផ្សេងៗមកដាក់បន្តគ្នា បង្កើតជាវត្ថុមួយថ្មីទៀត។
យើងអាចយកឃ្លាប្រយោគផ្សេងៗមកមកធ្វើប្រមាណវិធី concatenation operation ដូចខាងក្រោមនេះ៖
1 2 3 | sentence1 = 'Programming in Python ' sentence2 = 'is also solving problem.' print (sentence1 + sentence2) |
មួយវិញទៀត ដែលហៅថាប្រមាណវិធី repetition operation គឺជាប្រមាណវិធីទាំងឡាយណា ដែលនៅក្នុងនោះ មានការប្រើប្រាស់ប្រមាណសញ្ញា * ដើម្បីចំលងយកវត្ថុណាមួយ មកគុនអោយបានច្រើន រួចដាក់បន្តគ្នាបង្កើតជាវត្ថុថ្មីមួយទៀត។
1 2 3 | sentence = 'Drop by drop water breaks a stone.\n' print (sentence * 3 ) |
Drop by drop water breaks a stone.
Drop by drop water breaks a stone.
ឃ្លាប្រយោគគឺជា container បានន័យថា វាជាវត្ថុមួយដែលនៅក្នុងនោះមានវត្ថុជាច្រើនទៀត។ វត្ថុនៅក្នុងឃ្លាប្រយោគគឺជាតួអក្សរផ្សេងៗ ដែលត្រូវគេចាត់ទុកថាជា element នៃឃ្លាប្រយោគ។
តួអក្សរនៅក្នុងឃ្លាប្រយោគតំរៀបជួរគ្នាតាមលេខរៀង (index) ចាប់ពីសូន្យឡើងទៅ បើរាប់ពីដើមឃ្លាឡើងទៅ តែបើរាប់ពីចុងឃ្លាចុះមកវិញ តួអក្សរនៅខាងចុងគេ មានលេខរៀង -1 ។ ជាក់ស្តែង នៅក្នុងពាក្យថា Python តួអក្សរ P, y, t, h, o, n តំរៀបជួរគ្នាចាប់ពី 0 ឡើងទៅរហូត 5 បើយើងរាប់ចាប់ពីអក្សរ P ឡើងទៅ។ តែបើយើងរាប់ចាប់ពីអក្សរ n ចុះមកវិញ តួអក្សរទាំងនោះតំរៀបជួរគ្នាចាប់ពី -1 ចុះមករហូតដល់ -6 ។
នៅក្នុងភាសា Python ដែលហៅថាប្រមាណវិធី indexing operation គឺជាប្រមាណវិធីទាងឡាយណា ដែលនៅក្នុងនោះមានការប្រើប្រាស់ operator [ ] ដើម្បីចំលងយកធាតុណាមួយនៅក្នុងវត្ថុដែលជា container ណាមួយ មកប្រើការ។
យើងអាចយកឃ្លាប្រយោគផ្សេងៗមកធ្វើប្រមាណវិធី indexing opearation ដូចខាងក្រោមនេះ៖
1 2 3 4 5 | sentence = 'Drop by drop, water breaks a stone.' print (sentence[ 0 ]) print (sentence[ 5 ]) print (sentence[ - 3 ]) |
b
n
មួយវិញទៀត នៅក្នុងភាសា Python ដែលហៅថាប្រមាណវិធី slicing operation គឺជាប្រមាណវិធីទាំងឡាយណា ដែលនៅក្នុងនោះមានការប្រើប្រាស់ operator [:] ឬ [::] ដើម្បីចំលងយកធាតុមួយចំនួនពីសមាសវត្ថុណាមួយ មកបង្កើតជាសមាសវត្ថុមួយថ្មីទៀត។
យើងអាចយកឃ្លាប្រយោគផ្សេងៗ មកធ្វើប្រមាណវិធី slicing operation ដូចខាងក្រោមនេះ៖
1 2 3 4 5 | sentence = 'Drop by drop, water breaks a stone.' print (sentence[ 3 : 30 ]) print (sentence[: 30 ]) print (sentence[ 3 : 30 : 2 ]) |
Drop by drop, water breaks a s
pb rp ae rasas
ដូចនេះ បើ x, y, z ជាចំនួនគត់ និង sentence ជាឈ្មោះរបស់ឃ្លាប្រយោគណាមួយ ការសរសេរថា៖
- sentence[x:y] គឺជាការធ្វើប្រមាណវិធី slicing operation ដើម្បីចំលងយកអក្សរមួយចំនួន មានលេខរៀង ចាប់ពី x រហូតដល់ y-1 នៅក្នុងឃ្លាឈ្មោះ sentence មកបង្កើតជាឃ្លាថ្មីមួយទៀត។
- sentence[:y] គឺជាការធ្វើប្រមាណវិធី slicing operation ដើម្បីចំលងយកអក្សរមួយចំនួន មានលេខរៀង ចាប់ពី 0 រហូតដល់ y-1 នៅក្នុងឃ្លាឈ្មោះ sentence មកបង្កើតជាឃ្លាថ្មីមួយទៀត។
- sentence[x:] គឺជាការធ្វើប្រមាណវិធី slicing operation ដើម្បីចំលងយកអក្សរមួយចំនួន មានលេខរៀង ចាប់ពី x រហូតដល់តួអក្សរនៅខាងចុងគេបង្អស់ នៅក្នុងឃ្លាឈ្មោះ sentence មកបង្កើតជាឃ្លាថ្មីមួយទៀត។
- sentence[:] គឺជាការធ្វើប្រមាណវិធី slicing operation ដើម្បីចំលងយកតួអក្សរទាំងអស់ នៅក្នុងឃ្លាឈ្មោះ sentence មកបង្កើតជាឃ្លាថ្មីមួយទៀត។
- sentence[x:y:z] គឺជាការធ្វើប្រមាណវិធី slicing operation ដើម្បីចំលងយកអក្សរមួយចំនួន មានលេខរៀង ចាប់ពី x រហូតដល់ y-1 ដោយរំលងតួអក្សរចំនួន z-1 នៅក្នុងឃ្លាឈ្មោះ sentence មកបង្កើតជាឃ្លាថ្មីមួយទៀត។
យើងអាចយកឃ្លាប្រយោគផ្សេងៗ មកធ្វើប្រមាណវិធី logical operation ដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 | sentence1 = 'Drop by drop, water breaks a stone.' sentence2 = '' print (sentence1 and sentence2) print (sentence1 or sentence2) print ( not sentence1) print ( not sentence2) |
False
True
យើងអាចយកឃ្លាប្រយោគផ្សេងៗមកធ្វើប្រមាណវិធីប្រៀបធៀបដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 | name1 = 'Sokhavuth TIN' name2 = 'Guido Van Rossum' print (name1 = = name2) print (name1 ! = name2) print (name1 > name2) print (name1 < name2) print (name1 > = name2) print (name1 < = name2) |
True
True
False
True
False
នៅក្នុងការធ្វើប្រមាណវិធីប្រៀបធៀបរវាងឃ្លាប្រយោគឬពាក្យទាំងឡាយ ពាក្យពេចន៍ឬឃ្លាប្រយោគណាមួយធំជាងពាក្យឬឃ្លាប្រយោគមួយទៀត បានន័យថា បើសិនជាយើងយកពាក្យទាំងនោះ ទៅតំរៀបនៅក្នុងវចនានុក្រមណាមួយ ពាក្យដែលធំជាងគេត្រូវស្ថិតនៅខាងក្រោយពាក្យដែលតូចជាងគេ។
នៅក្នុងភាសា Python ដែលហៅថាប្រមាណវិធី membership operation គឺជាប្រមាណវិធីទាំងឡាយណា ដែលនៅក្នុងនោះមានការប្រើប្រាស់ operator in និង not in ដើម្បីពិនិត្យមើលថា តើនៅក្នងវត្ថុដែលជា container ណាមួយនោះ មានធាតុដូចគ្នាទៅនឹងវត្ថុណាមួយនោះដែរឬទេ។ លទ្ធផលបានមកពីប្រមាណវិធីនេះជា Boolean
យើងអាចយកឃ្លាប្រយោគផ្សេងៗ មកធ្វើប្រមាណវិធី membership operation ដូចខាងក្រោមនេះ៖
1 2 3 4 | sentence = 'Drop by drop water breaks a stone.' print ( 'Drop' in sentence) print ( 'Drop' not in sentence) |
False
នៅក្នុងភាសា Python កំណត់ពន្យល់ (comment) គឺជាឃ្លាប្រយោគទាំងឡាយដែលមានសញ្ញា # នៅពីមុខ និងត្រូវប្រើប្រាស់សំរាប់ពន្យល់ពីការប្រើប្រាស់បញ្ជាផ្សេងៗនៅក្នុងការសរសេរកម្មវិធីជាភាសា Python ។ ហើយនៅពេលដែលកម្មវិធីជាភាសា Python ដំណើរការ កំណត់ពន្យល់ទាំងឡាយនឹងត្រូវរំលងចោល ដោយផ្នែកទន់បកប្រែភាសា Python ។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 | #create a string named "sentence" sentence = 'Drop by drop water breaks a stone.' print (sentence) |