Skip to content

Instantly share code, notes, and snippets.

@uysalserkan
Created August 9, 2019 15:08
Show Gist options
  • Save uysalserkan/28eba103bf156ce6fbfe5353b102c3ee to your computer and use it in GitHub Desktop.
Save uysalserkan/28eba103bf156ce6fbfe5353b102c3ee to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# format() Metodunun Kullanım Biçimleri"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Herhangi bir yöne veya merkeze konumlandırmak ve alan ayırmak:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"|Volkan |\n"
]
}
],
"source": [
"string = \"|{:<15}|\".format(\"Volkan\")\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yukarıda görüldüğü gibi string sola yaslanmış biçimde fakat string için ayrılan bölme 15 birim."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| Öğrenciler|\n"
]
}
],
"source": [
"string = \"|{:>15}|\".format(\"Öğrenciler\")\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Yine yukarıda görüldüğü gibi string sağa yaslanmış biçimde fakat string için ayrılan bölme 15 birim."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sıra belirleme:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"isim soyisim\n"
]
}
],
"source": [
"string = \"{} {}\".format(\"isim\",\"soyisim\")\n",
"print(string)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"soyisim isim\n"
]
}
],
"source": [
"string = \"{1} {0}\".format(\"isim\",\"soyisim\")\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Görüldüğü gibi değişkenlerin geleceği sıraları ayarlayabiliyoruz."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## İsim Verme: "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mustafa Kemal ATATÜRK\n"
]
}
],
"source": [
"string = \"{ad} {soyad}\".format(soyad = \"ATATÜRK\",ad = \"Mustafa Kemal\")\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sıra ile yazmak mecburiyetinde değiliz, istersek değişken gibi isim verebiliyoruz."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Zorunluluğu:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"string = \"bu bir string {:s}\".format(str(12))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bunda hata almazken şunda alırız:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Unknown format code 's' for object of type 'int'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-c24a602ab5d9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"bu bir string {:s}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: Unknown format code 's' for object of type 'int'"
]
}
],
"source": [
"string = \"bu bir string {:s}\".format(12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sayı Zorunluluğu:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"string = \"burada sayı var {:d}\".format(25)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tabi eğer şöyle yazarsak hata alırız:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "Unknown format code 'd' for object of type 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-fb6a66e6a1fa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"burada sayı var {:d}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m25\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: Unknown format code 'd' for object of type 'str'"
]
}
],
"source": [
"string = \"burada sayı var {:d}\".format(str(25))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ASCII tablosundaki karşılıklar:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--->> A\n"
]
}
],
"source": [
"string = \"--->> {:c}\".format(65)\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"0 ile 256 arası sayıların ASCII tablosundaki karşılıklarını temsil eder."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sekizlik sistemdeki karşılıklar:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"string = \"{:o}\".format(16)\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Onaltılık sistemdeki karşılıklar:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2a\n"
]
}
],
"source": [
"string = \"{:x}\".format(42)\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Eğer büyük 'X' harfi kullanırsak harfler büyük gösterilir:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2A\n"
]
}
],
"source": [
"string = \"{:X}\".format(42)\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## İkilik sistemdeki karşılıklar:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"26 sayısının ikilik karşılığı -->> 11010\n"
]
}
],
"source": [
"string = \"26 sayısının ikilik karşılığı -->> {:b}\".format(26)\n",
"print(string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sayıları basamaklarına ayırma:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2,136,238,213,621\n"
]
}
],
"source": [
"string = \"{:,}\".format(2136238213621)\n",
"print(string)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment