Skip to content

Instantly share code, notes, and snippets.

@mde-2590
Created April 1, 2019 09:16
Show Gist options
  • Save mde-2590/da4afed7e86bbba0886deb3fcb9093f0 to your computer and use it in GitHub Desktop.
Save mde-2590/da4afed7e86bbba0886deb3fcb9093f0 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jackson'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use quotation marks for defining string\n",
"\n",
"\"Michael Jackson\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jackson'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use single quotation marks for defining string\n",
"\n",
"'Michael Jackson'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1 2 3 4 5 6 '"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Digitals and spaces in string\n",
"\n",
"'1 2 3 4 5 6 '"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'@#2_#]&*^%$'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Special characters in string\n",
"\n",
"'@#2_#]&*^%$'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello!\n"
]
}
],
"source": [
"# Print the string\n",
"\n",
"print(\"hello!\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jackson'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Assign string to variable\n",
"\n",
"Name = \"Michael Jackson\"\n",
"Name"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M\n"
]
}
],
"source": [
"# Print the first element in the string\n",
"\n",
"print(Name[0])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"l\n"
]
}
],
"source": [
"# Print the element on index 6 in the string\n",
"\n",
"print(Name[6])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"o\n"
]
}
],
"source": [
"# Print the element on the 13th index in the string\n",
"\n",
"print(Name[13])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n\n"
]
}
],
"source": [
"# Print the last element in the string\n",
"\n",
"print(Name[-1])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"M\n"
]
}
],
"source": [
"# Print the first element in the string\n",
"\n",
"print(Name[-15])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the length of string\n",
"\n",
"len(\"Michael Jackson\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Mich'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Take the slice on variable Name with only index 0 to index 3\n",
"\n",
"Name[0:4]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Jack'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Take the slice on variable Name with only index 8 to index 11\n",
"\n",
"Name[8:12]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'McalJcsn'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get every second element. The elments on index 1, 3, 5 ...\n",
"\n",
"Name[::2]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Mca'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get every second element in the range from index 0 to index 4\n",
"\n",
"Name[0:5:2]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jacksonis the best'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Concatenate two strings\n",
"\n",
"Statement = Name + \"is the best\"\n",
"Statement"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael JacksonMichael JacksonMichael Jackson'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print the string for 3 times\n",
"\n",
"3 * \"Michael Jackson\""
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jackson is the best'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Concatenate strings\n",
"\n",
"Name = \"Michael Jackson\"\n",
"Name = Name + \" is the best\"\n",
"Name"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Michael Jackson \n",
" is the best\n"
]
}
],
"source": [
"# New line escape sequence\n",
"\n",
"print(\" Michael Jackson \\n is the best\" )\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Michael Jackson \t is the best\n"
]
}
],
"source": [
"# Tab escape sequence\n",
"\n",
"print(\" Michael Jackson \\t is the best\" )"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Michael Jackson \\ is the best\n"
]
}
],
"source": [
"# Include back slash in string\n",
"\n",
"print(\" Michael Jackson \\\\ is the best\" )"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Michael Jackson \\ is the best\n"
]
}
],
"source": [
"# r will tell python that string will be display as raw string\n",
"\n",
"print(r\" Michael Jackson \\ is the best\" )"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"before upper: Thriller is the sixth studio album\n",
"After upper: THRILLER IS THE SIXTH STUDIO ALBUM\n"
]
}
],
"source": [
"# Convert all the characters in string to upper case\n",
"\n",
"A = \"Thriller is the sixth studio album\"\n",
"print(\"before upper:\", A)\n",
"B = A.upper()\n",
"print(\"After upper:\", B)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Janet Jackson is the best'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Replace the old substring with the new target substring is the segment has been found in the string\n",
"\n",
"A = \"Michael Jackson is the best\"\n",
"B = A.replace('Michael', 'Janet')\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the substring in the string. Only the index of the first elment of substring in string will be the output\n",
"\n",
"Name = \"Michael Jackson\"\n",
"Name.find('el')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the substring in the string.\n",
"\n",
"Name.find('Jack')"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# If cannot find the substring in the string\n",
"\n",
"Name.find('Jasdfasdasdf')"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"\n",
"A = \"1\"\n",
"print(\"1\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"B = \"2\"\n",
"print(\"2\")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'12'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute \n",
"\n",
"#C = A + B\n",
"\n",
"A = \"1\"\n",
"B = \"2\"\n",
"C = A + B\n",
"C"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ABC\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"D = \"ABCDEFG\"\n",
"#D[0:3]\n",
"\n",
"#Or\n",
"\n",
"print(D[0:3])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"correct\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"E = 'clocrkr1e1c1t'\n",
"print(E[::2])"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\\n",
" \\ \n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"print(\"\\\\\")\n",
"\n",
"#or\n",
"\n",
"print(r\" \\ \")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"before upper: You are wrong\n",
"After upper: YOU ARE WRONG\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"F = \"You are wrong\"\n",
"print(\"before upper:\", F)\n",
"F = F.upper()\n",
"print(\"After upper:\", F)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"95"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"G = \"Mary had a little lamb Little lamb, little lamb Mary had a little lamb \\\n",
"Its fleece was white as snow And everywhere that Mary went Mary went, Mary went \\\n",
"Everywhere that Mary went The lamb was sure to go\"\n",
"\n",
"G.find('snow')\n"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Bob had a little lamb Little lamb, little lamb Bob had a little lamb Its fleece was white as snow And everywhere that Bob went Bob went, Bob went Everywhere that Bob went The lamb was sure to go'"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"G.replace(\"Mary\", \"Bob\")"
]
},
{
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment