Introduction to Part 2 of “PowerShell Scripting guide to Python”

This is the second article in “PowerShell Scripting guide to Python” blog series.

If you are new please refer to the part-1 of this blog series for Python prerequisites and to get an idea of what has been covered.

The whole idea of this series is to make to link your existing knowledge of Powershell with new concept\syntax of Python as fast as possible.

In fast-changing IT scenarios to DevOps to Future (Data Science, AI, and Machine Learning) python is a must know.

We are covering following items in this article today

Overview

Date and Time Get Date and Time DateTime formatting Time Span or Time Delta

Strings Single, Double and Triple quoted strings String interpolation or variable substitution Escape characters Common string operations String formatting Substring or string slicing Built-in string methods



My Book – PowerShell Scripting to Python

Date and Time

Getting Date and Time Retrieving date and time in PowerShell is as simple a cmdlet Get-Date or using the [datetime] type accelerator But, Python date and time requires you to import time or datetime modules DateTime formatting Both PowerShell and Python provides Date and Time formatting with formatting codes.

Please find cheatsheets to the format codes in the following links– PowerShell date time string format codes

Python date time string format codes Achieve desirable date-time formatting by utilizing these Format codes. PowerShell- Python- Time Span or Time Delta In Powershell, a TimeSpan is a duration expressing the difference between two dates, time, or datetime.

TimeSpan can be used in multiple use cases like calculating a future or a past date

But, TimeDelta() is Python implementation of a TimeSpan which works exactly like [TimeSpan] with slightly different syntax

Strings and String Manipulations

Strings are most popular data types in Python can be created by simply enclosing characters in a single or double quote.

Single, Double and Triple quoted strings Single, double or triple quotes. Single quotes (‘) are used to create strings in Python, but Single Quotes are treated the same as double quotes (“) or triple-quoted string is used for creating multi-line strings. String interpolation or variable substitution String interpolation or variable substitution is achieved by Double quotes in PowerShell Python utilizes something called f-strings which were introduced in Python v3.6. In Python, we use F-strings for variable substitution



Escape characters Backslash ‘\’ is a representation of Escape characters in a Python string and can be interpreted in a single, double or triple quoted strings. Unlike Powershell which represents Escape character by backward apostrophe/grave ‘`’

PowerShell does not interpret Escape characters in Single Quotes, but only in Double quotes. Common string operations Common string operations in PowerShell and Python are almost similar Python – PowerShell – String formatting PowerShell provides a ‘-f’ operator called the Format operator to perform string formatting. Python has a similar Built-in .format() method for formatting the strings. Substring or string slicing Achieve Substring slicing or extracting from a Powershell string by Using Range operator

Using Substring() method Built-in string methods Python and PowerShell provide built-in methods for string manipulations, which are up to some extent similar in functionalities. Powershell Python

Hope you enjoyed reading this article and let me know your feedback. In the Part-3 of this blog series, we would be covering Data structures – List, Tuple, Dictionary, and Loops hence, please stay tuned!

More Articles from this Blog series –

Follow @SinghPrateik