Python: gunakan substrings - bagaimana ia berfungsi
Dalam hujung praktikal ini, kami menerangkan bagaimana anda boleh dengan mudah menggunakan substrings dalam Python. Kerana pengaturcaraan dengan Python tidak begitu sukar.
Gunakan substrings dalam Python
Substring adalah rentetan yang merupakan sebahagian daripada yang lain (atau lebih besar). Dalam Python, kaedah ini dipanggil "mengiris".
- Pertama sekali anda perlu menyimpan tali dengan pemboleh ubah. Contoh kami ialah: x = "Hello World!"
- Dengan arahan "x [2:]" anda kini boleh mengeluarkan rentetan tanpa dua digit pertama: "llo Dunia!"
- Dengan arahan "x [: 2]" pula dua digit pertama adalah output: "Hey"
- "X [: - 2]" perintah, yang menghasilkan rentetan tanpa dua digit terakhir, juga sangat praktikal: "Hello Worl"
- Ini juga berfungsi dengan cara yang lain, supaya hanya dua digit terakhir yang dihasilkan: "d!"
- Akhirnya, anda juga boleh menggabungkan arahan. Perintah "x [2: -2]" menghasilkan rentetan tanpa dua angka pertama dan terakhir, contohnya: "llo Worl"
Lebih banyak petua mengenai Python dalam panduan CHIP kami
Petua: Anda boleh mendapatkan lebih banyak tutorial pengaturcaraan pada halaman topik bahasa pemrograman Python kami. Di sana kami akan menunjukkan kepada anda, contohnya, bagaimana anda boleh memprogram GUI profesional.