Skip to content

Instantly share code, notes, and snippets.

@GuillaumeRouja
Created January 12, 2019 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GuillaumeRouja/f974a5dab0d0e1dd9de6d83261f45141 to your computer and use it in GitHub Desktop.
Save GuillaumeRouja/f974a5dab0d0e1dd9de6d83261f45141 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat_minor": 1,
"cells": [
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 1,
"metadata": {},
"data": {
"text/plain": "'Michael Jackson'"
},
"output_type": "execute_result"
}
],
"source": "# Assign string to variable\n\nName = \"Michael Jackson\"\nName"
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "l\n"
}
],
"source": "# Print the element on index 6 in the string\n\nprint(Name[6])"
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "n\n"
}
],
"source": "# Print the last element in the string\n\nprint(Name[-1])"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "M\n"
}
],
"source": "# Print the first element in the string\n\nprint(Name[-15])"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 5,
"metadata": {},
"data": {
"text/plain": "15"
},
"output_type": "execute_result"
}
],
"source": "# Find the length of string\n\nlen(\"Michael Jackson\")"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 6,
"metadata": {},
"data": {
"text/plain": "'Jack'"
},
"output_type": "execute_result"
}
],
"source": "# Take the slice on variable Name with only index 8 to index 11\n\nName[8:12]"
},
{
"execution_count": 7,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 7,
"metadata": {},
"data": {
"text/plain": "'McalJcsn'"
},
"output_type": "execute_result"
}
],
"source": "# Get every second element. The elments on index 1, 3, 5 ...\n\nName[::2]"
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 8,
"metadata": {},
"data": {
"text/plain": "'Mca'"
},
"output_type": "execute_result"
}
],
"source": "# Get every second element in the range from index 0 to index 4\n\nName[0:5:2]"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 10,
"metadata": {},
"data": {
"text/plain": "'Michael Jackson is the best'"
},
"output_type": "execute_result"
}
],
"source": "# Concatenate two strings\n\nStatement = Name + \" is the best\"\nStatement"
},
{
"execution_count": 12,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 12,
"metadata": {},
"data": {
"text/plain": "'Michael Jackson Michael Jackson Michael Jackson '"
},
"output_type": "execute_result"
}
],
"source": "# Print the string for 3 times\n\n3 * \"Michael Jackson \""
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": " Michael Jackson \n is the best\n"
}
],
"source": "# New line escape sequence\n\nprint(\" Michael Jackson \\n is the best\" )"
},
{
"execution_count": 14,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": " Michael Jackson \t is the best\n"
}
],
"source": "# Tab escape sequence\n\nprint(\" Michael Jackson \\t is the best\" )"
},
{
"execution_count": 15,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": " Michael Jackson \\ is the best\n"
}
],
"source": "# Include back slash in string\n\nprint(\" Michael Jackson \\\\ is the best\" )"
},
{
"execution_count": 16,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "before upper: Thriller is the sixth studio album\nAfter upper: THRILLER IS THE SIXTH STUDIO ALBUM\n"
}
],
"source": "# Convert all the characters in string to upper case\n\nA = \"Thriller is the sixth studio album\"\nprint(\"before upper:\", A)\nB = A.upper()\nprint(\"After upper:\", B)"
},
{
"execution_count": 17,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Guillaume is the best\n"
}
],
"source": "# Replace the old substring with the new target substring is the segment has been found in the string\n\nA = 'Michael is the best'\nB = A.replace('Michael','Guillaume')\nprint(B)"
},
{
"execution_count": 18,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 18,
"metadata": {},
"data": {
"text/plain": "5"
},
"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\nName = \"Michael Jackson\"\nName.find('el')"
},
{
"execution_count": 19,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 19,
"metadata": {},
"data": {
"text/plain": "-1"
},
"output_type": "execute_result"
}
],
"source": "# If cannot find the substring in the string, result is -1\n\nName.find('Jasdfasdasdf')"
},
{
"execution_count": 21,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 21,
"metadata": {},
"data": {
"text/plain": "'12'"
},
"output_type": "execute_result"
}
],
"source": "A='1'\nB='2'\nC=A+B\nC"
},
{
"execution_count": 24,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 24,
"metadata": {},
"data": {
"text/plain": "'ABC'"
},
"output_type": "execute_result"
}
],
"source": "#Consider the variable D use slicing to print out the first three elements:\n\nD=\"ABCDEF\"\nD[:3]"
},
{
"execution_count": 26,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 26,
"metadata": {},
"data": {
"text/plain": "'correct'"
},
"output_type": "execute_result"
}
],
"source": "#Use a stride value of 2 to print out every second character of the string E: \n\nE = 'clocrkr1e1c1t'\nE[::2]\n"
},
{
"execution_count": 29,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "\\\n"
}
],
"source": "#Print out a backslash:\nprint(\"\\\\\")"
},
{
"execution_count": 33,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 33,
"metadata": {},
"data": {
"text/plain": "'YOU ARE WRONG'"
},
"output_type": "execute_result"
}
],
"source": "#Convert the variable F to uppercase:\nF=\"You are wrong\"\nF.upper()\n\n"
},
{
"execution_count": 36,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 36,
"metadata": {},
"data": {
"text/plain": "95"
},
"output_type": "execute_result"
}
],
"source": "#Consider the variable G, and find the first index of the sub-string snow:\n\nG = \"Mary had a little lamb Little lamb, little lamb Mary had a little lamb \\\nIts fleece was white as snow And everywhere that Mary went Mary went, Mary went \\\nEverywhere that Mary went The lamb was sure to go\"\n\nG.find(\"snow\")"
},
{
"execution_count": 37,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 37,
"metadata": {},
"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'"
},
"output_type": "execute_result"
}
],
"source": "#In the variable G, replace the sub-string Mary with Bob:\n\nG.replace('Mary','Bob')"
},
{
"execution_count": null,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.5",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.5",
"name": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment