Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save laresbernardo/4c2ce79cdfe3a5354a75f9d5f866346a to your computer and use it in GitHub Desktop.
Save laresbernardo/4c2ce79cdfe3a5354a75f9d5f866346a to your computer and use it in GitHub Desktop.
strings and text
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Define a text for the examples\nword = \"Hello world\"",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Extract characters based on position (first position = 0)\nprint(word[2:5]) # From Ath to Bth char\nprint(word[-3:]) # The last n-1 char\nprint(word[3:]) # All except the first 3 char\nprint(word[:-3]) # All except the last 3 char",
"execution_count": 24,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "llo\nrld\nlo world\nHello wo\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Length of a text\nlen(word)",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "11"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Place where a textual text is (first one to show)\nprint(\"Letter 'o' is @\",word.find(\"o\"))\nprint(\"Word 'world' starts @\", word.index(\"world\"))",
"execution_count": 107,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "Letter %d is @ 4\nWord 'world' starts @ 6\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Frequency of a text\nprint(\"O's:\", word.count('o'))\nprint(\"Hello's:\", word.count('Hello'))",
"execution_count": 14,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "O's: 2\nHello's: 1\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Split texts\nword.split(' ')",
"execution_count": 50,
"outputs": [
{
"data": {
"text/plain": "['Hello', 'world']"
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Replace string\nword.replace(\"Hello\", \"Goodbye\")",
"execution_count": 32,
"outputs": [
{
"data": {
"text/plain": "'Goodbye world'"
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Changing Upper and Lower Case Strings\nprint(word.upper()) # All upper case\nprint(word.lower()) # All lower case\nprint(word.title()) # All first letters in upper case\nprint(word.capitalize()) # Just first letter upper case",
"execution_count": 36,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "HELLO WORLD\nhello world\nHello World\nHello world\n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Repeat text n times\nprint(\":) | \"* 10)",
"execution_count": 47,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": ":) | :) | :) | :) | :) | :) | :) | :) | :) | :) | \n"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Some boolean checks\nprint(word.isalnum()) # Check if all char are alphanumeric \nprint(word.isalpha()) # Check if all char in the string are alphabetic\nprint(word.isdigit()) # Test if string contains digits\nprint(word.istitle()) # Test if string contains title words\nprint(word.isupper()) # Test if string contains upper case\nprint(word.islower()) # Test if string contains lower case\nprint(word.isspace()) # Test if string consists on spaces\nprint(word.endswith('d')) # Test if string endswith\nprint(word.startswith('H')) # Test if string startswith ",
"execution_count": 198,
"outputs": [
{
"output_type": "stream",
"text": "False\nFalse\nFalse\nFalse\nFalse\nFalse\nFalse\nTrue\nTrue\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Contains text in another text\ntexts = ['Primer texto','Segundo texto','Tercer']\nprint(\"'text' is in the first row:\",\"text\" in texts[0]) # Is a text in a text\nprint(\"'Cuarto' is somewhere:\",\"Cuarto\" in texts) # Is there a row that is",
"execution_count": 199,
"outputs": [
{
"output_type": "stream",
"text": "'text' is in the first row: True\n'Cuarto' is somewhere: False\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Filter rows which contains\nlist(filter(lambda x:'tex' in x, texts))",
"execution_count": 206,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 206,
"data": {
"text/plain": "['Primer texto', 'Segundo texto']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Contains text in each row in a list\ncontains_text = []\nfor i in range(len(texts)):\n contains_text.append(\"text\" in texts[i])\nprint(contains_text)",
"execution_count": 200,
"outputs": [
{
"output_type": "stream",
"text": "[True, True, False]\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/4bf65e993789abfbeee2e52331c90c20"
},
"gist": {
"id": "4bf65e993789abfbeee2e52331c90c20",
"data": {
"description": "strings and text",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment