Skip to content

Instantly share code, notes, and snippets.

@saboyutaka
Last active February 3, 2017 06:55
Show Gist options
  • Save saboyutaka/b0af68f6b50bd0bf3921efde0082f64c to your computer and use it in GitHub Desktop.
Save saboyutaka/b0af68f6b50bd0bf3921efde0082f64c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 数値というデータ型"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n",
"<class 'int'>\n",
"<class 'int'>\n"
]
}
],
"source": [
"print(type(\"Hello World\"))\n",
"print(type(10))\n",
"\n",
"x = 10\n",
"print(type(x))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'float'>\n"
]
}
],
"source": [
"print(type(10.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 数値型で遊んで見る"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"2\n",
"4\n"
]
}
],
"source": [
"print(2 + 3 - 1)\n",
"print(3-1)\n",
"print(2 + 3 - 1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"256\n",
"8\n",
"4.0\n",
"2.4\n",
"2.4\n"
]
}
],
"source": [
"print(2**8)\n",
"print(2**3)\n",
"print(12/3)\n",
"print(12/5)\n",
"print(12.0/5.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 数値型と文字列型の取り扱いの違い"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n"
]
}
],
"source": [
"print(type(\"39\"))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(\"I am 39 years old\".find(\"39\"))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "Can't convert 'int' object to str implicitly",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-25-bda2d14309ae>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"I am 39 years old\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfind\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m39\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly"
]
}
],
"source": [
"print(\"I am 39 years old\".find(39))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(\"I am 39 years old\".find(str(39)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cast キャスト 型を変化する int()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21\n"
]
}
],
"source": [
"x = \"3\"\n",
"y = \"7\"\n",
"print(int(x) * int(y))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'str'>\n"
]
}
],
"source": [
"print(type(str(int(str(39)))))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"It's 6 PM.\n"
]
}
],
"source": [
"print(\"It's\", 6, \"PM.\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "TypeError",
"evalue": "Can't convert 'int' object to str implicitly",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-36-53343dc5e3b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"It's\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m6\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\"PM.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly"
]
}
],
"source": [
"print(\"It's\" + 6 + \"PM.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 変数にいれた文字列を整形して表示してみる(文字列フォーマット)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### format()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John.\n"
]
}
],
"source": [
"x = \"John\"\n",
"print(\"I am \" + x + \".\")\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John\n"
]
}
],
"source": [
"print(\"I am {}\".format(\"John\"))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John\n"
]
}
],
"source": [
"x = \"John\"\n",
"print(\"I am {}\".format(x))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John. You are Yoko\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = \"Yoko\"\n",
"print(\"I am {}. You are {}\".format(x, y))"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John, 58 years old.\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = 58\n",
"print(\"I am {}, {} years old.\".format(x, y))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John, John is my name.\n"
]
}
],
"source": [
"x = \"John\"\n",
"print(\"I am {0}, {0} is my name.\".format(x))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John, Tokyo is John's hometown.\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = \"Tokyo\"\n",
"print(\"I am {0}, {1} is {0}'s hometown.\".format(x, y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### %x\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John.\n"
]
}
],
"source": [
"x = \"John\"\n",
"print(\"I am %s.\" % x)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John. You are Yoko\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = \"Yoko\"\n",
"print(\"I am %s. You are %s\" % (x, y))"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am Yoko. You are John\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = \"Yoko\"\n",
"print(\"I am %s. You are %s\" % (y, x))"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John, 28 years old.\n"
]
}
],
"source": [
"x = \"John\"\n",
"y = 28\n",
"print(\"I am %s, %d years old.\" % (x, y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 文字列を整形して表示してみる(改行, center, ljust, rjust)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John. I am happy.\n"
]
}
],
"source": [
"print(\"I am John. I am happy.\")"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I am John. \n",
"I am happy.\n"
]
}
],
"source": [
"print(\"I am John. \\nI am happy.\")"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" THE MAN \n",
" John\n",
"My name is John. I live in Tokyo\n"
]
}
],
"source": [
"print(\"THE MAN\".center(30))\n",
"print(\"John\".rjust(30))\n",
"print(\"My name is John. I live in Tokyo\".ljust(30))"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" HOMETOWN\n",
" John\n",
"Hello. I am Jon. Tokyo\n",
"is my home town.\n",
" Jan 20 2015\n",
" John\n",
"\n"
]
}
],
"source": [
"print(\"\"\"\n",
" HOMETOWN\n",
" John\n",
"Hello. I am Jon. Tokyo\n",
"is my home town.\n",
" Jan 20 2015\n",
" John\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(11)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 補足説明 dir, help"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dir\n",
"与えられた引数のclassが持つmethodの配列を表示するコマンド"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getnewargs__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mod__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__rmod__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'capitalize',\n",
" 'casefold',\n",
" 'center',\n",
" 'count',\n",
" 'encode',\n",
" 'endswith',\n",
" 'expandtabs',\n",
" 'find',\n",
" 'format',\n",
" 'format_map',\n",
" 'index',\n",
" 'isalnum',\n",
" 'isalpha',\n",
" 'isdecimal',\n",
" 'isdigit',\n",
" 'isidentifier',\n",
" 'islower',\n",
" 'isnumeric',\n",
" 'isprintable',\n",
" 'isspace',\n",
" 'istitle',\n",
" 'isupper',\n",
" 'join',\n",
" 'ljust',\n",
" 'lower',\n",
" 'lstrip',\n",
" 'maketrans',\n",
" 'partition',\n",
" 'replace',\n",
" 'rfind',\n",
" 'rindex',\n",
" 'rjust',\n",
" 'rpartition',\n",
" 'rsplit',\n",
" 'rstrip',\n",
" 'split',\n",
" 'splitlines',\n",
" 'startswith',\n",
" 'strip',\n",
" 'swapcase',\n",
" 'title',\n",
" 'translate',\n",
" 'upper',\n",
" 'zfill']"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# str が持つメソッドが表示される\n",
"dir(\"hello world\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## help\n",
"引数に与えられたクラス、またはmethodのdocmentを表示する\n",
"help(class) \n",
"help(class.method)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class str in module builtins:\n",
"\n",
"class str(object)\n",
" | str(object='') -> str\n",
" | str(bytes_or_buffer[, encoding[, errors]]) -> str\n",
" | \n",
" | Create a new string object from the given object. If encoding or\n",
" | errors is specified, then the object must expose a data buffer\n",
" | that will be decoded using the given encoding and error handler.\n",
" | Otherwise, returns the result of object.__str__() (if defined)\n",
" | or repr(object).\n",
" | encoding defaults to sys.getdefaultencoding().\n",
" | errors defaults to 'strict'.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __add__(self, value, /)\n",
" | Return self+value.\n",
" | \n",
" | __contains__(self, key, /)\n",
" | Return key in self.\n",
" | \n",
" | __eq__(self, value, /)\n",
" | Return self==value.\n",
" | \n",
" | __format__(...)\n",
" | S.__format__(format_spec) -> str\n",
" | \n",
" | Return a formatted version of S as described by format_spec.\n",
" | \n",
" | __ge__(self, value, /)\n",
" | Return self>=value.\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __getitem__(self, key, /)\n",
" | Return self[key].\n",
" | \n",
" | __getnewargs__(...)\n",
" | \n",
" | __gt__(self, value, /)\n",
" | Return self>value.\n",
" | \n",
" | __hash__(self, /)\n",
" | Return hash(self).\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __le__(self, value, /)\n",
" | Return self<=value.\n",
" | \n",
" | __len__(self, /)\n",
" | Return len(self).\n",
" | \n",
" | __lt__(self, value, /)\n",
" | Return self<value.\n",
" | \n",
" | __mod__(self, value, /)\n",
" | Return self%value.\n",
" | \n",
" | __mul__(self, value, /)\n",
" | Return self*value.n\n",
" | \n",
" | __ne__(self, value, /)\n",
" | Return self!=value.\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | __rmod__(self, value, /)\n",
" | Return value%self.\n",
" | \n",
" | __rmul__(self, value, /)\n",
" | Return self*value.\n",
" | \n",
" | __sizeof__(...)\n",
" | S.__sizeof__() -> size of S in memory, in bytes\n",
" | \n",
" | __str__(self, /)\n",
" | Return str(self).\n",
" | \n",
" | capitalize(...)\n",
" | S.capitalize() -> str\n",
" | \n",
" | Return a capitalized version of S, i.e. make the first character\n",
" | have upper case and the rest lower case.\n",
" | \n",
" | casefold(...)\n",
" | S.casefold() -> str\n",
" | \n",
" | Return a version of S suitable for caseless comparisons.\n",
" | \n",
" | center(...)\n",
" | S.center(width[, fillchar]) -> str\n",
" | \n",
" | Return S centered in a string of length width. Padding is\n",
" | done using the specified fill character (default is a space)\n",
" | \n",
" | count(...)\n",
" | S.count(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the number of non-overlapping occurrences of substring sub in\n",
" | string S[start:end]. Optional arguments start and end are\n",
" | interpreted as in slice notation.\n",
" | \n",
" | encode(...)\n",
" | S.encode(encoding='utf-8', errors='strict') -> bytes\n",
" | \n",
" | Encode S using the codec registered for encoding. Default encoding\n",
" | is 'utf-8'. errors may be given to set a different error\n",
" | handling scheme. Default is 'strict' meaning that encoding errors raise\n",
" | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n",
" | 'xmlcharrefreplace' as well as any other name registered with\n",
" | codecs.register_error that can handle UnicodeEncodeErrors.\n",
" | \n",
" | endswith(...)\n",
" | S.endswith(suffix[, start[, end]]) -> bool\n",
" | \n",
" | Return True if S ends with the specified suffix, False otherwise.\n",
" | With optional start, test S beginning at that position.\n",
" | With optional end, stop comparing S at that position.\n",
" | suffix can also be a tuple of strings to try.\n",
" | \n",
" | expandtabs(...)\n",
" | S.expandtabs(tabsize=8) -> str\n",
" | \n",
" | Return a copy of S where all tab characters are expanded using spaces.\n",
" | If tabsize is not given, a tab size of 8 characters is assumed.\n",
" | \n",
" | find(...)\n",
" | S.find(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the lowest index in S where substring sub is found,\n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Return -1 on failure.\n",
" | \n",
" | format(...)\n",
" | S.format(*args, **kwargs) -> str\n",
" | \n",
" | Return a formatted version of S, using substitutions from args and kwargs.\n",
" | The substitutions are identified by braces ('{' and '}').\n",
" | \n",
" | format_map(...)\n",
" | S.format_map(mapping) -> str\n",
" | \n",
" | Return a formatted version of S, using substitutions from mapping.\n",
" | The substitutions are identified by braces ('{' and '}').\n",
" | \n",
" | index(...)\n",
" | S.index(sub[, start[, end]]) -> int\n",
" | \n",
" | Like S.find() but raise ValueError when the substring is not found.\n",
" | \n",
" | isalnum(...)\n",
" | S.isalnum() -> bool\n",
" | \n",
" | Return True if all characters in S are alphanumeric\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isalpha(...)\n",
" | S.isalpha() -> bool\n",
" | \n",
" | Return True if all characters in S are alphabetic\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isdecimal(...)\n",
" | S.isdecimal() -> bool\n",
" | \n",
" | Return True if there are only decimal characters in S,\n",
" | False otherwise.\n",
" | \n",
" | isdigit(...)\n",
" | S.isdigit() -> bool\n",
" | \n",
" | Return True if all characters in S are digits\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | isidentifier(...)\n",
" | S.isidentifier() -> bool\n",
" | \n",
" | Return True if S is a valid identifier according\n",
" | to the language definition.\n",
" | \n",
" | Use keyword.iskeyword() to test for reserved identifiers\n",
" | such as \"def\" and \"class\".\n",
" | \n",
" | islower(...)\n",
" | S.islower() -> bool\n",
" | \n",
" | Return True if all cased characters in S are lowercase and there is\n",
" | at least one cased character in S, False otherwise.\n",
" | \n",
" | isnumeric(...)\n",
" | S.isnumeric() -> bool\n",
" | \n",
" | Return True if there are only numeric characters in S,\n",
" | False otherwise.\n",
" | \n",
" | isprintable(...)\n",
" | S.isprintable() -> bool\n",
" | \n",
" | Return True if all characters in S are considered\n",
" | printable in repr() or S is empty, False otherwise.\n",
" | \n",
" | isspace(...)\n",
" | S.isspace() -> bool\n",
" | \n",
" | Return True if all characters in S are whitespace\n",
" | and there is at least one character in S, False otherwise.\n",
" | \n",
" | istitle(...)\n",
" | S.istitle() -> bool\n",
" | \n",
" | Return True if S is a titlecased string and there is at least one\n",
" | character in S, i.e. upper- and titlecase characters may only\n",
" | follow uncased characters and lowercase characters only cased ones.\n",
" | Return False otherwise.\n",
" | \n",
" | isupper(...)\n",
" | S.isupper() -> bool\n",
" | \n",
" | Return True if all cased characters in S are uppercase and there is\n",
" | at least one cased character in S, False otherwise.\n",
" | \n",
" | join(...)\n",
" | S.join(iterable) -> str\n",
" | \n",
" | Return a string which is the concatenation of the strings in the\n",
" | iterable. The separator between elements is S.\n",
" | \n",
" | ljust(...)\n",
" | S.ljust(width[, fillchar]) -> str\n",
" | \n",
" | Return S left-justified in a Unicode string of length width. Padding is\n",
" | done using the specified fill character (default is a space).\n",
" | \n",
" | lower(...)\n",
" | S.lower() -> str\n",
" | \n",
" | Return a copy of the string S converted to lowercase.\n",
" | \n",
" | lstrip(...)\n",
" | S.lstrip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with leading whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | partition(...)\n",
" | S.partition(sep) -> (head, sep, tail)\n",
" | \n",
" | Search for the separator sep in S, and return the part before it,\n",
" | the separator itself, and the part after it. If the separator is not\n",
" | found, return S and two empty strings.\n",
" | \n",
" | replace(...)\n",
" | S.replace(old, new[, count]) -> str\n",
" | \n",
" | Return a copy of S with all occurrences of substring\n",
" | old replaced by new. If the optional argument count is\n",
" | given, only the first count occurrences are replaced.\n",
" | \n",
" | rfind(...)\n",
" | S.rfind(sub[, start[, end]]) -> int\n",
" | \n",
" | Return the highest index in S where substring sub is found,\n",
" | such that sub is contained within S[start:end]. Optional\n",
" | arguments start and end are interpreted as in slice notation.\n",
" | \n",
" | Return -1 on failure.\n",
" | \n",
" | rindex(...)\n",
" | S.rindex(sub[, start[, end]]) -> int\n",
" | \n",
" | Like S.rfind() but raise ValueError when the substring is not found.\n",
" | \n",
" | rjust(...)\n",
" | S.rjust(width[, fillchar]) -> str\n",
" | \n",
" | Return S right-justified in a string of length width. Padding is\n",
" | done using the specified fill character (default is a space).\n",
" | \n",
" | rpartition(...)\n",
" | S.rpartition(sep) -> (head, sep, tail)\n",
" | \n",
" | Search for the separator sep in S, starting at the end of S, and return\n",
" | the part before it, the separator itself, and the part after it. If the\n",
" | separator is not found, return two empty strings and S.\n",
" | \n",
" | rsplit(...)\n",
" | S.rsplit(sep=None, maxsplit=-1) -> list of strings\n",
" | \n",
" | Return a list of the words in S, using sep as the\n",
" | delimiter string, starting at the end of the string and\n",
" | working to the front. If maxsplit is given, at most maxsplit\n",
" | splits are done. If sep is not specified, any whitespace string\n",
" | is a separator.\n",
" | \n",
" | rstrip(...)\n",
" | S.rstrip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with trailing whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | split(...)\n",
" | S.split(sep=None, maxsplit=-1) -> list of strings\n",
" | \n",
" | Return a list of the words in S, using sep as the\n",
" | delimiter string. If maxsplit is given, at most maxsplit\n",
" | splits are done. If sep is not specified or is None, any\n",
" | whitespace string is a separator and empty strings are\n",
" | removed from the result.\n",
" | \n",
" | splitlines(...)\n",
" | S.splitlines([keepends]) -> list of strings\n",
" | \n",
" | Return a list of the lines in S, breaking at line boundaries.\n",
" | Line breaks are not included in the resulting list unless keepends\n",
" | is given and true.\n",
" | \n",
" | startswith(...)\n",
" | S.startswith(prefix[, start[, end]]) -> bool\n",
" | \n",
" | Return True if S starts with the specified prefix, False otherwise.\n",
" | With optional start, test S beginning at that position.\n",
" | With optional end, stop comparing S at that position.\n",
" | prefix can also be a tuple of strings to try.\n",
" | \n",
" | strip(...)\n",
" | S.strip([chars]) -> str\n",
" | \n",
" | Return a copy of the string S with leading and trailing\n",
" | whitespace removed.\n",
" | If chars is given and not None, remove characters in chars instead.\n",
" | \n",
" | swapcase(...)\n",
" | S.swapcase() -> str\n",
" | \n",
" | Return a copy of S with uppercase characters converted to lowercase\n",
" | and vice versa.\n",
" | \n",
" | title(...)\n",
" | S.title() -> str\n",
" | \n",
" | Return a titlecased version of S, i.e. words start with title case\n",
" | characters, all remaining cased characters have lower case.\n",
" | \n",
" | translate(...)\n",
" | S.translate(table) -> str\n",
" | \n",
" | Return a copy of the string S in which each character has been mapped\n",
" | through the given translation table. The table must implement\n",
" | lookup/indexing via __getitem__, for instance a dictionary or list,\n",
" | mapping Unicode ordinals to Unicode ordinals, strings, or None. If\n",
" | this operation raises LookupError, the character is left untouched.\n",
" | Characters mapped to None are deleted.\n",
" | \n",
" | upper(...)\n",
" | S.upper() -> str\n",
" | \n",
" | Return a copy of S converted to uppercase.\n",
" | \n",
" | zfill(...)\n",
" | S.zfill(width) -> str\n",
" | \n",
" | Pad a numeric string S with zeros on the left, to fill a field\n",
" | of the specified width. The string S is never truncated.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods defined here:\n",
" | \n",
" | maketrans(x, y=None, z=None, /)\n",
" | Return a translation table usable for str.translate().\n",
" | \n",
" | If there is only one argument, it must be a dictionary mapping Unicode\n",
" | ordinals (integers) or characters to Unicode ordinals, strings or None.\n",
" | Character keys will be then converted to ordinals.\n",
" | If there are two arguments, they must be strings of equal length, and\n",
" | in the resulting dictionary, each character in x will be mapped to the\n",
" | character at the same position in y. If there is a third argument, it\n",
" | must be a string, whose characters will be mapped to None in the result.\n",
"\n"
]
}
],
"source": [
"# strのdocumentを表示\n",
"help(\"\")\n",
"\n",
"# help(srt) でも同じ\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function upper:\n",
"\n",
"upper(...) method of builtins.str instance\n",
" S.upper() -> str\n",
" \n",
" Return a copy of S converted to uppercase.\n",
"\n"
]
}
],
"source": [
"help(\"\".upper)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dirの一覧からmethodを選択して使ってみる\n",
"`__sample__` のように__で囲まれたmethodは内部で使われるメソッドなので普段は使わない"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"STARTUP CAFE\n",
"Startup Cafe\n",
" startup cafe \n",
"startup cafe\n"
]
}
],
"source": [
"print(\"startup cafe\".upper())\n",
"print(\"startup cafe\".title())\n",
"print(\" startup cafe \")\n",
"print(\" startup cafe \".strip())"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"False\n",
"False\n"
]
}
],
"source": [
"print(\"start\".islower())\n",
"print(\"Start\".islower())\n",
"print(\"cafe\".isupper())\n",
"print(\"Cafe\".isupper())"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n",
"False\n",
"True\n"
]
}
],
"source": [
"print(\"Start\".startswith(\"s\"))\n",
"print(\"start\".startswith(\"s\"))\n",
"print(\"start\".endswith(\"s\"))\n",
"print(\"start\".endswith(\"t\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment