Cara menggunakan python print table align

Secara default bawaan HTML, setiap tabel akan diatur lebarnya secara otomatis dari besar data yang ada didalam tabel tersebut, namun jika kita ingin ‘memaksakan’ web browser untuk menampilkan tabel dengan lebar tertentu, kita bisa menambahkan sebuah atribut width untuk mengatur lebar tabel.

Cara menggunakan python print table align

Dapat dilihat bahwa web browser menampilkan tabel sesuai dengan panjang data yang terdapat pada baris terpanjang, dan jika anda mencoba mengecilkan jendela web browser, tampilan tabel akan bergeser menyesuaikan dengan lebar web browser. Inilah tampilan default dari tabel HTML.


Mengatur Lebar Tabel Dengan Atribut Width

Atribut width dapat digunakan untuk tag tabel (tag table) maupun untuk tag kolom (tag th, tag td, maupun tag col). Jika diletakkan pada tag table, atribut ini berfungsi untuk mengatur lebar tabel secara keseluruhan. Namun jika diletakkan pada tag th atau td, atribut ini akan berfungsi untuk mengatur lebar kolom.

Untuk “memaksa” web browser menampilkan lebar tabel sesuai dengan yang diinginkan, kita dapat menambahkan atribut width pada tag table, seperti contoh berikut:

Contoh penggunaan atribut width tabel HTML, tabelwidth.html

<!DOCTYPE html>
<html>
   <head>
      <title>Contoh pemakaian attribut width dalam Tabel HTML</title>
   </head>
<body>
<h2>Belajar atribut width dalam Tabel HTML</h2>
<table border="1" width="600px">
<caption>------------Lebar Tabel 600px--------------</caption>
    <tr>
        <th>Judul 1</th>
        <th>Judul 2</th>
        <th>Judul 3</th>
    </tr>
    <tr>
        <td>Baris 1, Kolom 1</td>
        <td>Baris 1, Kolom 2</td>
        <td>Baris 1, Kolom 3</td>
    </tr>
    <tr>
        <td>Data yang sangat panjang, 
            sehingga merusak tampilan dari tabel</td>
        <td>Baris 2, Kolom 2</td>
        <td>Baris 2, Kolom 3</td>
    </tr>
    <tr>
        <td>Baris 3, Kolom 1</td>
        <td>Baris 3, Kolom 2</td>
        <td>Baris 3, Kolom 3</td>
    </tr>
</table>
</body>
</html>

Cara menggunakan python print table align

Jika anda mencoba mengubah kembali ukuran web browser, tampilan tabel akan tetap, sesuai dengan nilai dari atribut width.

Atribut width dapat berisi nilai fixed (tetap), yakni dalam satuan pixel (misalnya: 400px, 600px), maupun nilai relatif dalam bentuk persen (misalnya: 30%, 60%).

Cara menggunakan python print table align

Seperti yang terlihat, bahwa lebar dari masing-masing kolom diset melalui nilai atribut width.

img

Sebagai catatan, seandainya total lebar dari seluruh kolom (atribut width pada tag th) melebihi lebar tabel (atribut width pada tag table), maka lebar masing-masing kolom akan ‘disesuaikan’ agar tidak melebihi lebar tabel. Sehingga untuk menghindari tampilan yang tidak diinginkan, pastikan agar total lebar kolom tidak melebihi nilai width tabel.

Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string.

Approach : We will be using the f-strings to format the text. The syntax of the alignment of the output string is defined by ‘<‘, ‘>’, ‘^’ and followed by the width number.

Example 1 : For Left Alignment output string syntax define ‘<‘ followed by the width number.




# here 20 spaces are reserved for the 

# particular output string. And the string

# is printed on the left side

print(f

  Right Aligned Text
0
  Right Aligned Text
1

Output :

Left Aligned Text

Example 2 : For Right Alignment output string syntax define ‘>’ followed by the width number.




# here 20 spaces are reserved for the 

# particular output string. And the string

  Right Aligned Text
4

print(f

  Right Aligned Text
7
  Right Aligned Text
1

Output :

  Right Aligned Text

Example 3 : For Center Alignment output string syntax define ‘^’ followed by the width number.




# here 20 spaces are reserved for the 

# particular output string. And the string

 Centered 
1

print(f

 Centered 
4
  Right Aligned Text
1

Output :

 Centered 

Example 4 : Printing variables in Aligned format




 Centered 
6

 Centered 
7
 Centered 
8
 Centered 
9

Left Text            Centered Text           Right Text
0
 Centered 
8
Left Text            Centered Text           Right Text
2

Left Text            Centered Text           Right Text
3
 Centered 
8
Left Text            Centered Text           Right Text
5

Left Text            Centered Text           Right Text

Left Text            Centered Text           Right Text
7

print(f

Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
0
  Right Aligned Text
1

Output :

Left Text            Centered Text           Right Text

Example 5 : Printing out multiple list values in aligned column look.




Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
2

Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
3
 Centered 
8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
5
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
6
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# here 20 spaces are reserved for the 0
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# here 20 spaces are reserved for the 2# here 20 spaces are reserved for the 3

# here 20 spaces are reserved for the 4

 Centered 
8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
5# here 20 spaces are reserved for the 7
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# here 20 spaces are reserved for the 9
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# particular output string. And the string1
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# particular output string. And the string3# here 20 spaces are reserved for the 3

# particular output string. And the string5

 Centered 
8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
5# particular output string. And the string8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# particular output string. And the string8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# is printed on the left side2
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7# is printed on the left side4# here 20 spaces are reserved for the 3

# is printed on the left side6

 Centered 
8
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
5# is printed on the left side9
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7print1
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7print3
Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38
7print5# here 20 spaces are reserved for the 3