Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Examples
  • Now you try it!
  1. Language Basics

Lesson 5. String Concatenation

One way you can change the value of a variable if it is a string value is to take it and smoosh it together with another string. This is called string concatenation and it comes in handy when you need to do things like build file paths.

Examples

Smoosh!

When you assign a string value to a variable, you let Python know it is a string by surrounding it in quotes. You can use single quotes or double quotes. Just be sure to be consistent. As you can see below, spaces are signified by a space in between two quotes.

name = 'Bob'

print(name)

name = name + ' ' + 'Wakefield'

print(name)

Building File Paths

At some point you are probably going to need to move files around on a machine. Some Python methods require that you specify a file name while others just need a directory.

In either case, you want to build a string out of your file path in such a way that you get maximum flexibility out of the whole string. You do that by breaking up the file path into things that change and things that stay the same.

In the variables tutorial, you saw the sophisticated way to do this. I am going to show you a more brute force approach.

See how we break the string down so it is three component parts? In the real world, you will frequently write processes where the file name will change as you loop through a directory. I have simulated that here by reassigning the variable file_name to a new value.

Also notice how I have written the double backslashes. That is because the backslash has special meaning in Python. The backslash is used to denote that a special character is coming like \n which represents the ASCII linefeed value. The backslash escapes the n so the Python interpreter knows that \n means LF and not just the literal string '\n'.

Since backslash is the escape character, putting another backslash in front of a backslash “escapes” the first backslash and now the Python interpreter will know to treat the initial backslash like any other string.

website_projects_dir = '\\User\\Directory\\Projects\\Website'
tutorial_images_dir = '\\Copy\\Academics\\Tutorials\\images'
file_name = '\\sql.jpg'
abs_file_path = website_projects_dir + tutorial_images_dir + file_name

print(abs_file_path)

file_name = '\\python.jpg'
abs_file_path = website_projects_dir + tutorial_images_dir + file_name

print(abs_file_path)

Now you try it!

Don't copy and past. Type the code yourself!

PreviousLesson 4. VariablesNextLesson 6. Arithmetic Operators

Last updated 3 years ago