\n",
"1. Get the character that index is 0 in x = \"Clark is pretty good\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.2 String Concatenation"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LevelUp\n",
"Level Up\n"
]
}
],
"source": [
"a = \"Level\"\n",
"b = \"Up\"\n",
"c = a + b\n",
"print(c)\n",
"d = a + ' ' + b\n",
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.3 Combine of string and number"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[9], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m36\u001b[39m\n\u001b[1;32m----> 2\u001b[0m txt \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMy name is John, I am \u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m age\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(txt)\n",
"\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"age = 36\n",
"txt = \"My name is John, I am \" + age\n",
"print(txt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Exercise 2: Type conversion
\n",
"\n",
"1. We have used built-in function like `print()` and `type()` to do task, this time we use `str()` to convert a value to string \n",
"2. Use `str()` function to remove the error"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I have lived here for 5 years\n"
]
}
],
"source": [
"# F-Strings\n",
"age = 5\n",
"txt = f\"I have lived here for {age} years\"\n",
"print(txt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.4 Methods for String\n",
"A **method** is a special type of function, it allow you to do different things with different objects.\n",
"\n",
"We can use methods through **dot notation**: `variable.method()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"names = \"Clark\"\n",
"u_names = names.upper()\n",
"print(u_names)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Methods for String in Python Documentation](https://docs.python.org/3/library/stdtypes.html#string-methods)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"