{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# İleri Seviye Karakter Dizileri (Stringler)\n", "\n", "\n", "Bu konuda karakter dizilerinin yararlı metodlarını öğrenmeye çalışacağız." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### upper() ve lower()\n", "\n", "***upper()*** metodu stringin tüm karakterlerini büyük harfe çevirir.\n", "\n", "***lower()*** metodu stringin tüm karakterlerini küçük harfe çevirir." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'PYTHON'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"python\".upper()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"PYTHON\".lower()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'PYTHON'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"PythOn\".upper()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"PythOn\".lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### replace() \n", "\n", "***replace(x,y)*** : Stringteki x değerlerini y ile değiştirir. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Herkes ono bobo bocı gordoş'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Herkes ana baba bacı gardaş\".replace(\"a\",\"o\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Kunzun'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Kunduz\".replace(\"duz\",\"zun\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Python-Programlama-Dili'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Python Programlama Dili\".replace(\" \",\"-\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### startswith() ve endswith()\n", "\n", "***startswith(x)*** : String x ile başlıyorsa True, başlamıyorsa False değeri döndürür.\n", "\n", "***endswith(x)*** : String x ile bitiyorsa True, bitmiyorsa False değeri döndürür." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Python\".startswith(\"py\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Python\".startswith(\"Py\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Python\".endswith(\"on\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Python\".endswith(\"az\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### split()\n", "\n", "***split(a)*** : Verilen bir a değerine göre string parçalara ayrılarak herbir parça listeye atılır." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = \"Python Programlama Dili\".split(\" \") # Boşluk karakterine göre ayrıldı." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'Programlama', 'Dili']\n" ] } ], "source": [ "print(liste)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste2 = \"Python-Php-Java-C-Javascript\".split(\"-\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'Php', 'Java', 'C', 'Javascript']\n" ] } ], "source": [ "print(liste2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### strip() , lstrip() ve rstrip()\n", "\n", "***strip(x)*** : Stringin başında ve sonunda bulunan x değerlerini siler.\n", "\n", "***lstrip(x)*** : Stringin sadece başında bulunan x değerlerini siler.\n", "\n", "***rstrip(x)*** : Stringin sadece sonunda bulunan x değerlerini siler." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" python \".strip() # değer göndermezsek varsayılan olarak boşluk karakteri" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mustafa'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\">>>>>>>>>>>>>>Mustafa>>>>>>>>>>>>>>>>>>>>>>>>>>\".strip(\">\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python '" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" python \".lstrip()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' python'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" python \".rstrip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### join()\n", "\n", "Listenin elemanlarını bir string değeriyle birleştirmemizi sağlar." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [\"21\",\"02\",\"2014\"]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'21/02/2014'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"/\".join(liste)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [\"T\",\"B\",\"M\",\"M\"]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'T.B.M.M'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\".\".join(liste)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### count() \n", "\n", "***count(x)***: Stringin içindeki x değerlerini sayar.\n", "\n", "***count(x,index)***: Stringin içindeki x değerlerini verilen index değerinden başlayarak saymaya başlar." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"ada kapısı yandan çarklı\".count(\"a\")" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"ada kapısı yandan çarklı\".count(\" \")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"ada kapısı yandan çarklı\".count(\"a\",2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### find() ve rfind()\n", "\n", "***find(x)*** : x değerini baştan itibaren string içinde arar ve bulursa ilk bulduğu indeksi döndürür. Bulamazsa \"-1\" değerini verir.\n", "\n", "***rfind(x)*** : x değerini sondan itibaren string içinde arar ve bulursa ilk bulduğu indeksi döndürür. Bulamazsa \"-1\" değerini verir." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"araba\".find(\"a\")" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"araba\".find(\"s\")" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"araba\".rfind(\"a\")" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"araba\".rfind(\"s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte stringlerin belli başlı metodları bunlardır. Diğer metodları da ihtiyacınız olduğu durumda şu siteye bakarak öğrenebilirsiniz. \n", "\n", "https://www.tutorialspoint.com/python3/python_strings.htm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }