Python: izmantojiet apakšvirknes - kā tā darbojas
Šajā praktiskajā padomā mēs izskaidrojam, kā Python var viegli izmantot apakšvirknes. Tā kā programmēšana ar Python nav tik grūta.
Izmantojiet apakšpozīcijas Python
Apakšstīga ir virkne, kas ir daļa no citas (vai lielākas). Pythonā šo metodi sauc par "sagriešanu".
- Pirmkārt, jums ir jāuzglabā virkne ar mainīgo. Mūsu piemērs ir: x = "Sveika pasaule!"
- Ar komandu "x [2:]" jūs tagad varat izvadīt virkni bez pirmajiem diviem cipariem: "llo World!"
- Ar komandu "x [: 2]" savukārt tiek izvadīti tikai pirmie divi cipari: "Hey"
- Ļoti praktiska ir arī komanda "x [: - 2]", kas izvada virkni bez pēdējiem diviem cipariem: "Hello Worl"
- Tas darbojas arī otrādi, tā, ka tiek izvadīti tikai pēdējie divi cipari: "d!"
- Visbeidzot, jūs varat arī apvienot komandas. Komanda "x [2: -2]" izvada virkni bez pirmajiem un pēdējiem diviem cipariem, piemēram: "llo Worl"
Vairāk padomu par Python mūsu CHIP ceļvežos
Padoms. Plašākas programmēšanas pamācības varat atrast mūsu Python programmēšanas valodas tēmu lapā. Tur mēs, piemēram, parādīsim, kā jūs varat ieprogrammēt profesionālu GUI.