Tutorial Python

RUMAH Python Pengenalan Python Python Memulai Sintaks Python Komentar Python Variabel Python Tipe Data Python Nomor Python Pengecoran Python String Python Python Boolean Operator Python Daftar Python Tuple Python Set Python Kamus Python Python Jika...Lain Python Sementara Loop Python Untuk Loop Fungsi Python Python Lambda Array Python Kelas/Objek Python Warisan Python Python Iterator Lingkup Python Modul Python Tanggal Python Python Matematika Python JSON Python RegEx Python PIP Python Coba...Kecuali Masukan Pengguna Python Pemformatan String Python

Penanganan Berkas

Penanganan File Python File Baca Python Python Tulis/Buat File Python Hapus File

Modul Python

Tutorial NumPy Panduan Panda Tutorial sip

Python Matplotlib

Pengantar Matplotlib Matplotlib Memulai Matplotlib Pyplot Merencanakan Matplotlib Penanda Matplotlib Garis Matplotlib Label Matplotlib Kotak Matplotlib Subplot Matplotlib Penyebaran Matplotlib Matplotlib Bar Histogram Matplotlib Bagan Pai Matplotlib

Pembelajaran mesin

Mulai Mode Median Rata-rata Standar Deviasi Persentil Distribusi Data Distribusi Data Normal Plot Pencar Regresi linier Regresi Polinomial Regresi Berganda Skala Kereta/Tes Pohon Keputusan

Python MySQL

MySQL Memulai MySQL Buat Basis Data MySQL Buat Tabel Sisipan MySQL MySQL Pilih MySQL Dimana MySQL Dipesan Oleh Hapus MySQL Tabel Drop MySQL Pembaruan MySQL Batas MySQL MySQL Bergabung

Python MongoDB

MongoDB Memulai MongoDB Buat Basis Data MongoDB Buat Koleksi Sisipan MongoDB Temukan MongoDB Permintaan MongoDB Sortir MongoDB Hapus MongoDB Koleksi Jatuhkan MongoDB Pembaruan MongoDB Batas MongoDB

Referensi Python

Ikhtisar Python Fungsi bawaan Python Metode String Python Metode Daftar Python Metode Kamus Python Metode Tuple Python Metode Set Python Metode File Python Kata Kunci Python Pengecualian Python Daftar Istilah Python

Referensi Modul

Modul Acak Modul Permintaan Modul Statistik Modul Matematika Modul cMath

Python Bagaimana caranya?

Hapus Duplikat Daftar Membalikkan String Tambahkan Dua Angka

Contoh Python

Contoh Python Kompilator Python Latihan Python Kuis Python Sertifikat Python

Format String Python () Metode

Metode String


Contoh

Masukkan harga di dalam placeholder, harga harus dalam format titik tetap, dua desimal:

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))

Definisi dan Penggunaan

Metode format()ini memformat nilai yang ditentukan dan memasukkannya ke dalam placeholder string.

Placeholder didefinisikan menggunakan tanda kurung kurawal: {}. Baca lebih lanjut tentang placeholder di bagian Placeholder di bawah ini.

Metode format()mengembalikan string yang diformat.


Sintaksis

string.format(value1, value2...)

Nilai Parameter

Parameter Description
value1, value2... Required. One or more values that should be formatted and inserted in the string.

The values are either a list of values separated by commas, a key=value list, or a combination of both.

The values can be of any data type.

Placeholder

Placeholder dapat diidentifikasi menggunakan indeks bernama {price}, indeks bernomor {0}, atau bahkan placeholder kosong {}.

Contoh

Menggunakan nilai placeholder yang berbeda:

txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36)

Jenis Pemformatan

Di dalam placeholder Anda dapat menambahkan tipe pemformatan untuk memformat hasil:

:< 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

Metode String