String Formatting

In this section we will learn various approaches to format strings in Python and PowerShell and what are the similarities between these approaches.

Format Operator in PowerShell : -f

PowerShell provides a '-f' operator called as the Format operator to perform string formatting, where left-hand side of operator is a String with placeholders denoted by squiggly brackets ( {} ) and right-hand side is an array of values to place into the place holder string on the left.

Syntax:

"this is {0} a string {1}" -f $value1, $value2

It is a must to define each placeholder with the index of element on right hand side so that the values are placed correctly, first element of the array of values start at '0'

Python String Formatting : .format()

Python has a similar Built-in .format() method for formatting the strings, which can be applied on any string on the left using the Dot (.) operator and accepts values as arguments that will passed into the placeholders in the string.

Syntax:

"this is {} a string {}".format(value1, value2)

Alternatively with index numbers

"this is {0} a string {1}".format(value1, value2)

It is not mandatory to define the index inside a placeholder and Python automatically assumes the placeholder value in the sequence they are defined in format() method as a argument.


NOTE: 

This is just a teaser content from my book in form of a blog post!

If you want to know more about Common String Operations, String Interpolation, Escape characters, String slicing (sub-string) and built-in string methods in Python and PowerShell, then read my book (below) which is still in progress, on lean publishing format.

Buy early, pay less, free updates!


My new book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at prateek@ridicurious.com

Optical Character Recognition

Subscribe to our mailing list

* indicates required