Python data structure basics

2022/7/27 14:24:06

本文主要是介绍Python data structure basics,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Program to reverse a string

String = "My name is Miguel."
print(String[::-1])

Python String join() Method

The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

Deleting a String with the use of del

del String

Escape Sequencing of String

String = 'I\'m Miguel.'
print(String)

Using raw String to ignore Escape Sequences

String1 = r"This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"
print(String1)

Formatting of Strings

# Default order
String1 = "{} {} {}".format('Geeks', 'For', 'Life')
print("Print String in default order: ")
print(String1)
 
# Positional Formatting
String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')
print("\nPrint String in Positional order: ")
print(String1)
 
# Keyword Formatting
String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')
print("\nPrint String in order of Keywords: ")
print(String1)

Integers such as Binary, hexadecimal, etc., and floats can be rounded or displayed in the exponent form with the use of format specifiers.

# Formatting of Integers
String1 = "{0:b}".format(16)
print("\nBinary representation of 16 is ")
print(String1)
 
# Formatting of Floats
String1 = "{0:e}".format(165.6458)
print("\nExponent representation of 165.6458 is ")
print(String1)
 
# Rounding off Integers
String1 = "{0:.2f}".format(1/6)
print("\none-sixth is : ")
print(String1)


这篇关于Python data structure basics的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程