Skip to content

Commit

Permalink
Add string operations
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Aug 18, 2024
1 parent c84e64b commit 75ecbbc
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 0 deletions.
1 change: 1 addition & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parts:
- file: book/python/01_get_started
- file: book/python/02_variables
- file: book/python/03_data_structures
- file: book/python/04_string_operations
- caption: GeoPython
numbered: true
maxdepth: 2
Expand Down
10 changes: 10 additions & 0 deletions book/python/02_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ centroid = [centroid_lat, centroid_lon]
print("Centroid of the points is at:", centroid)
```

## Exercise

1. Create a list of tuples, each representing the coordinates (latitude, longitude) of different cities you have visited.
2. Calculate the centroid of these coordinates.
3. Create a dictionary to store the centroid's latitude and longitude.

```{code-cell}
# Type your code here
```

## Conclusion

Understanding Python variables and data types is crucial in geospatial programming.
Expand Down
218 changes: 218 additions & 0 deletions book/python/04_string_operations.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "bcb4d025",
"metadata": {},
"source": [
"# String Operations\n",
"\n",
"In this notebook, we will explore various string operations in Python and see how they can be applied in geospatial contexts. Understanding how to manipulate and analyze strings is essential when working with geographic data, such as handling names of places, formatting coordinates, and parsing data from text files."
]
},
{
"cell_type": "markdown",
"id": "b8d33eeb",
"metadata": {},
"source": [
"## Creating and Manipulating Strings\n",
"\n",
"Strings in Python are sequences of characters. You can create a string by enclosing characters in single or double quotes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ef1fb56",
"metadata": {},
"outputs": [],
"source": [
"location_name = \"Mount Everest\" # A string representing the name of a location"
]
},
{
"cell_type": "markdown",
"id": "f5e965b3",
"metadata": {},
"source": [
"You can concatenate (join) strings using the `+` operator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42526b7a",
"metadata": {},
"outputs": [],
"source": [
"location_name_full = location_name + \", Nepal\"\n",
"print(location_name_full)"
]
},
{
"cell_type": "markdown",
"id": "eae07d43",
"metadata": {},
"source": [
"You can also repeat strings using the `*` operator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508b11d4",
"metadata": {},
"outputs": [],
"source": [
"separator = \"-\" * 10\n",
"print(separator)"
]
},
{
"cell_type": "markdown",
"id": "5669c026",
"metadata": {},
"source": [
"## String Methods for Geospatial Data\n",
"\n",
"Python provides various built-in methods to manipulate strings. Some commonly used methods include:\n",
"- `lower()`, `upper()`: Convert strings to lowercase or uppercase.\n",
"- `strip()`: Remove leading and trailing whitespace.\n",
"- `replace()`: Replace a substring with another substring.\n",
"- `split()`: Split a string into a list of substrings based on a delimiter."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "caefbc57",
"metadata": {},
"outputs": [],
"source": [
"location_name_upper = location_name.upper()\n",
"print(location_name_upper) # Convert to uppercase"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14c657bd",
"metadata": {},
"outputs": [],
"source": [
"location_name_clean = location_name.strip()\n",
"print(location_name_clean) # Remove leading/trailing whitespace"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "640f1423",
"metadata": {},
"outputs": [],
"source": [
"location_name_replaced = location_name.replace(\"Everest\", \"K2\")\n",
"print(location_name_replaced) # Replace 'Everest' with 'K2'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "827dd307",
"metadata": {},
"outputs": [],
"source": [
"location_parts = location_name_full.split(\", \")\n",
"print(location_parts) # Split the string into a list"
]
},
{
"cell_type": "markdown",
"id": "b24b43e0",
"metadata": {},
"source": [
"## Formatting Strings\n",
"\n",
"String formatting is essential when preparing data for output or when you need to include variable values in strings. You can use the `format()` method or f-strings (in Python 3.6 and above) for string formatting."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "248322a2",
"metadata": {},
"outputs": [],
"source": [
"latitude = 27.9881\n",
"longitude = 86.9250\n",
"formatted_coordinates = \"Coordinates: ({}, {})\".format(latitude, longitude)\n",
"print(formatted_coordinates)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "616a001f",
"metadata": {},
"outputs": [],
"source": [
"formatted_coordinates_fstring = f\"Coordinates: ({latitude}, {longitude})\"\n",
"print(formatted_coordinates_fstring)"
]
},
{
"cell_type": "markdown",
"id": "58262850",
"metadata": {},
"source": [
"## Parsing and Extracting Information from Strings\n",
"\n",
"Often, you will need to extract specific information from strings, especially when dealing with geographic data. For example, you might need to extract coordinates from a formatted string."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc54dbed",
"metadata": {},
"outputs": [],
"source": [
"coordinate_string = \"27.9881N, 86.9250E\"\n",
"lat_str, lon_str = coordinate_string.split(\", \")\n",
"latitude = float(lat_str[:-1]) # Convert string to float and remove the 'N'\n",
"longitude = float(lon_str[:-1]) # Convert string to float and remove the 'E'\n",
"print(f\"Parsed coordinates: ({latitude}, {longitude})\")"
]
},
{
"cell_type": "markdown",
"id": "6f54635f",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"1. Create a string representing the name of a city. Convert the string to lowercase and then to uppercase.\n",
"2. Take a string with the format 'latitude, longitude' (e.g., '40.7128N, 74.0060W') and extract the numeric values of latitude and longitude.\n",
"3. Create a formatted string that includes the name of a location and its coordinates. Use both the `format()` method and f-strings to achieve this.\n",
"4. Replace a substring in the name of a place (e.g., change 'San Francisco' to 'San Diego') and print the result."
]
},
{
"cell_type": "markdown",
"id": "f910f3f0",
"metadata": {},
"source": [
"### Conclusion\n",
"\n",
"String operations are crucial in geospatial programming, especially when dealing with textual geographic data. Mastering these operations will enable you to handle and manipulate geographic information effectively in your projects."
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
110 changes: 110 additions & 0 deletions book/python/04_string_operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
jupytext:
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.16.2
kernelspec:
display_name: geo
language: python
name: python3
---

# String Operations

In this notebook, we will explore various string operations in Python and see how they can be applied in geospatial contexts. Understanding how to manipulate and analyze strings is essential when working with geographic data, such as handling names of places, formatting coordinates, and parsing data from text files.

+++

## Creating and Manipulating Strings

Strings in Python are sequences of characters. You can create a string by enclosing characters in single or double quotes.

```{code-cell}
location_name = "Mount Everest" # A string representing the name of a location
```

You can concatenate (join) strings using the `+` operator:

```{code-cell}
location_name_full = location_name + ", Nepal"
print(location_name_full)
```

You can also repeat strings using the `*` operator:

```{code-cell}
separator = "-" * 10
print(separator)
```

## String Methods for Geospatial Data

Python provides various built-in methods to manipulate strings. Some commonly used methods include:

- `lower()`, `upper()`: Convert strings to lowercase or uppercase.
- `strip()`: Remove leading and trailing whitespace.
- `replace()`: Replace a substring with another substring.
- `split()`: Split a string into a list of substrings based on a delimiter.

```{code-cell}
location_name_upper = location_name.upper()
print(location_name_upper) # Convert to uppercase
```

```{code-cell}
location_name_clean = location_name.strip()
print(location_name_clean) # Remove leading/trailing whitespace
```

```{code-cell}
location_name_replaced = location_name.replace("Everest", "K2")
print(location_name_replaced) # Replace 'Everest' with 'K2'
```

```{code-cell}
location_parts = location_name_full.split(", ")
print(location_parts) # Split the string into a list
```

## Formatting Strings

String formatting is essential when preparing data for output or when you need to include variable values in strings. You can use the `format()` method or f-strings (in Python 3.6 and above) for string formatting.

```{code-cell}
latitude = 27.9881
longitude = 86.9250
formatted_coordinates = "Coordinates: ({}, {})".format(latitude, longitude)
print(formatted_coordinates)
```

```{code-cell}
formatted_coordinates_fstring = f"Coordinates: ({latitude}, {longitude})"
print(formatted_coordinates_fstring)
```

## Parsing and Extracting Information from Strings

Often, you will need to extract specific information from strings, especially when dealing with geographic data. For example, you might need to extract coordinates from a formatted string.

```{code-cell}
coordinate_string = "27.9881N, 86.9250E"
lat_str, lon_str = coordinate_string.split(", ")
latitude = float(lat_str[:-1]) # Convert string to float and remove the 'N'
longitude = float(lon_str[:-1]) # Convert string to float and remove the 'E'
print(f"Parsed coordinates: ({latitude}, {longitude})")
```

## Exercises

1. Create a string representing the name of a city. Convert the string to lowercase and then to uppercase.
2. Take a string with the format 'latitude, longitude' (e.g., '40.7128N, 74.0060W') and extract the numeric values of latitude and longitude.
3. Create a formatted string that includes the name of a location and its coordinates. Use both the `format()` method and f-strings to achieve this.
4. Replace a substring in the name of a place (e.g., change 'San Francisco' to 'San Diego') and print the result.

+++

### Conclusion

String operations are crucial in geospatial programming, especially when dealing with textual geographic data. Mastering these operations will enable you to handle and manipulate geographic information effectively in your projects.

0 comments on commit 75ecbbc

Please sign in to comment.