Skip to content

Instantly share code, notes, and snippets.

@masasa27
Created August 1, 2020 18:43
Show Gist options
  • Select an option

  • Save masasa27/da8dad2ed95f37d46143f47143160dae to your computer and use it in GitHub Desktop.

Select an option

Save masasa27/da8dad2ed95f37d46143f47143160dae to your computer and use it in GitHub Desktop.
strings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Strings"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"my_string = \"Is 'Hello World' still interesting?\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operations"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Is 'Hello World' still interesting?\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_string"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Addition"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Is 'Hello World' still interesting?\n",
"i don't know, you tell me!\n"
]
}
],
"source": [
"# '\\n' = adding a new line!\n",
"# strings can be open with \"\" or ''\n",
"print(my_string + '\\n' + \"i don't know, you tell me!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Searching"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"start = my_string.find('Hello World') # the starting location of the \"hello world\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"word_length = len('Hello World') # how many letters does this strings has"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"end = start + word_length"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sub-Strings"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Is'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_string[0:2] # this is asking a substring from my_string, from location 0 (the start) to location 2 not including"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_string[start:end] # sub string!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment