-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Strings\n", | ||
"\n", | ||
"Some examples of strings methods" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 63, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"['gattaca', 'MMBDTP', 'charles darwin at home', '122']\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"SomeStrings = [\"gattaca\", \"MMBDTP\", \"charles darwin at home\", \"122\"]\n", | ||
"print(SomeStrings)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can use a **loop** to print each element of the list:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 64, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"gattaca\n", | ||
"MMBDTP\n", | ||
"charles darwin at home\n", | ||
"122\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for string in SomeStrings:\n", | ||
" print(string)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"`len()` returns the length of the string:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 65, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"gattaca 7\n", | ||
"MMBDTP 6\n", | ||
"charles darwin at home 22\n", | ||
"122 3\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for string in SomeStrings:\n", | ||
" print(string, len(string))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can convert the string to **uppercase** or **lowercase** (this can be useful for example when searching motifs in a query sequence avoiding case sensitivity traps):" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 66, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"gattaca -> GATTACA -> gattaca -> Gattaca\n", | ||
"MMBDTP -> MMBDTP -> mmbdtp -> Mmbdtp\n", | ||
"charles darwin at home -> CHARLES DARWIN AT HOME -> charles darwin at home -> Charles darwin at home\n", | ||
"122 -> 122 -> 122 -> 122\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for string in SomeStrings:\n", | ||
" print(string, \" -> \", string.upper(),\" -> \", string.lower(),\" -> \", string.capitalize())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"is the string a number?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 67, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"gattaca \n", | ||
"\t is decimal? False \n", | ||
"\t is digit? False \n", | ||
"\t is numeric? False\n", | ||
"MMBDTP \n", | ||
"\t is decimal? False \n", | ||
"\t is digit? False \n", | ||
"\t is numeric? False\n", | ||
"charles darwin at home \n", | ||
"\t is decimal? False \n", | ||
"\t is digit? False \n", | ||
"\t is numeric? False\n", | ||
"122 \n", | ||
"\t is decimal? True \n", | ||
"\t is digit? True \n", | ||
"\t is numeric? True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for string in SomeStrings:\n", | ||
" print(string, \"\\n\\t is decimal?\", string.isdecimal(),\"\\n\\t is digit?\", string.isdigit(), \"\\n\\t is numeric?\", string.isnumeric())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Substrings?\n", | ||
"\n", | ||
"The `find()` method will return the (zero based) positio of a substring:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 68, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"'gattaca': Found at position 1\n", | ||
"'MMBDTP': 'at' not found\n", | ||
"'charles darwin at home': Found at position 15\n", | ||
"'122': 'at' not found\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for string in SomeStrings:\n", | ||
" if string.find(\"at\") == -1:\n", | ||
" print(f\"'{string}': 'at' not found\")\n", | ||
" else:\n", | ||
" print(f\"'{string}': Found at position {string.find('at')}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can also **replace** strings" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 70, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The first string is gattaca\n", | ||
"Replace 'a' with 'A' in the first string: gXttXcX\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"The first string is\", SomeStrings[0])\n", | ||
"print(\"Replace 'a' with 'A' in the first string:\", SomeStrings[0].replace(\"a\", \"X\"))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "pystart", | ||
"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.10.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |