{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\">Strings</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ord('T'), chr(84)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"'' in 'A string', 'str' in 'A string', 'St' not in 'A string'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(len('Is this REALLY worth $38.20?'))\n",
"print('Is this REALLY worth $38.20?'[8])\n",
"print('Is this REALLY worth $38.20?'[8: 15])\n",
"print('Is this REALLY worth $38.20?'[8: 15: 2])\n",
"print('Is this' + ' REALLY worth ' + '$38.20?')\n",
"print('Is this' ' REALLY worth '\n",
" '$38.20?')\n",
"print('$38.20' * 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"S = 'Is this REALLY worth $38.20?'\n",
"print(S.casefold())\n",
"print(S.lower())\n",
"print(S.capitalize())\n",
"print(S.title())\n",
"print(S.upper())\n",
"print(S.swapcase())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"S = 'Is this REALLY worth $38.20?'\n",
"# Next six strings have a width of 50\n",
"print(S.center(50))\n",
"print(S.center(50, '_'))\n",
"print(S.ljust(50))\n",
"print(S.ljust(50, '_'))\n",
"print(S.rjust(50))\n",
"print(S.rjust(50, '_'))\n",
"print()\n",
"\n",
"print('-123.45'.zfill(4))\n",
"print('-123.45'.zfill(12))\n",
"print()\n",
"\n",
"print('Use \\t to insert a tab.')\n",
"# The default, which can be changed, is to replace a tab by 8 spaces\n",
"print('Use \\t to insert a tab.'.expandtabs())\n",
"print('Use \\t to insert a tab.'.expandtabs(12))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(' \\n\\t A string with leading and trailing spaces \\t\\n '.strip())\n",
"print(' \\n\\t A string with leading and trailing spaces \\t\\n '.lstrip())\n",
"print('***')\n",
"print(' \\n\\t A string with leading and trailing spaces \\t\\n '.rstrip())\n",
"print('Another string without leading and trailing spaces'.strip('eAconsiw'))\n",
"print('Another string without leading and trailing spaces'.lstrip('eAconsiw'))\n",
"print('Another string without leading and trailing spaces'.rstrip('eAconsiw'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Forêt'.isalpha())\n",
"print('größer_123'.islower())\n",
"print('ΕΛΛΑΣ$><$!'.isupper())\n",
"print(' String Reference 1345 Mrfc…'.istitle())\n",
"print('0123'.isdecimal())\n",
"print('0123'.isdigit())\n",
"print('\\u00B2 is also a digit')\n",
"print('0123'.isnumeric())\n",
"print('\\u00BD is also numeric')\n",
"print('012AbC'.isalnum())\n",
"print('No_digit_such_as_8_in_a_Python_identifier'.isidentifier())\n",
"print(' \\t\\n\\f\\r'.isspace())\n",
"print('˜µ≥!a9溧…≈≥!'.isprintable())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('baabababababaab'.count('aba'))\n",
"print('baabababababaab'.count('aba', 3))\n",
"print('baabababababaab'.count('aba', 3, 11))\n",
"print('baabababababaab'.count('aba', 3, 10))\n",
"print()\n",
"\n",
"print('baabababababaab'.find('aba'))\n",
"print('baabababababaab'.find('aba', 3))\n",
"print('baabababababaab'.find('aba', 3, 7))\n",
"print('baabababababaab'.find('aba', 3, 6))\n",
"print('baabababababaab'.index('aba'))\n",
"print('baabababababaab'.index('aba', 3))\n",
"print('baabababababaab'.index('aba', 3, 7))\n",
"print()\n",
"\n",
"print('baabababababaab'.rfind('aba'))\n",
"print('baabababababaab'.rfind('aba', -4))\n",
"print('baabababababaab'.rfind('aba', 1, 8))\n",
"print('baabababababaab'.rindex('aba'))\n",
"print('baabababababaab'.rindex('aba', 1, 8))\n",
"print()\n",
"\n",
"print('baabababababaab'.startswith('baab'))\n",
"print('baabababababaab'.endswith('abaab'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('baabababababaab'.replace('aba', '_NEW_'))\n",
"print('baabababababaab'.replace('aba', '_NEW_', 2))\n",
"print()\n",
"\n",
"# 99 is the Unicode ordinal of character c\n",
"print('abcdefgabcdefg'.translate(str.maketrans({'a': '_A_', 99: '_C_', 'd': None })))\n",
"print('abcdefgabcdefg'.translate(str.maketrans('acd', 'ACD')))\n",
"print('abcdefgabcdefg'.translate(str.maketrans('acd', 'ACD', 'ef')))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('abc;defg;,hijkl;,mnop'.partition(';,'))\n",
"print('abc;defg;,hijkl;,mnop'.partition(',;'))\n",
"print()\n",
"\n",
"print('abc;defg;,hijkl;,mnop'.rpartition(';,'))\n",
"print('abc;defg;,hijkl;,mnop'.rpartition(',;'))\n",
"print()\n",
"\n",
"print('abc;defg;,hijkl;,mnop'.split(';,'))\n",
"print('abc;defg;,hijkl;,mnop'.split(';,', 1))\n",
"print(' \\n\\t abc \\n\\t defg \\n\\t hijkl \\n\\t mnop \\n\\t '.split())\n",
"print(' \\n\\t abc \\n\\t defg \\n\\t hijkl \\n\\t mnop \\n\\t '.split(maxsplit = 1))\n",
"print()\n",
"\n",
"print('abc;defg;,hijkl;,mnop'.rsplit(';,'))\n",
"print('abc;defg;,hijkl;,mnop'.rsplit(';,', 1))\n",
"print(' \\n\\t abc \\n\\t defg \\n\\t hijkl \\n\\t mnop \\n\\t '.rsplit())\n",
"print(' \\n\\t abc \\n\\t defg \\n\\t hijkl \\n\\t mnop \\n\\t '.rsplit(maxsplit = 1))\n",
"print()\n",
"print('''adc\n",
" defg\n",
" hijkl\n",
" mnop'''.splitlines())\n",
"print('''adc\n",
" defg\n",
" hijkl\n",
" mnop'''.splitlines(True))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('_*_'.join(['a', 'bc', 'de', 'f']))\n",
"print('_*_'.join(('a', 'bc', 'de', 'f')))\n",
"print('_*_'.join({'a', 'bc', 'de', 'f'}))\n",
"print('_*_'.join({'a' : 1, 'bc' : 2, 'de': 3, 'f': 4}))"
]
}
],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Resource created Wednesday 19 August 2015, 10:05:47 AM.
file: strings.ipynb