How are escape sequences contributing to the length of a string? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
a=("\nHello")
print(len(a))
The intended output is a new line with 5 printed but instead there is no new line and the length is coming out to be 6.

There is a new line printed, plus the 5 characters in 'Hello', that's 6 in total:
a = "\nHello"
print(a)
print("Total length is", len(a))
Output
<space>
Hello
Total length is 6

Related

Format String - number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a easy question but i need explanation.
In this code Why the output is 100:
x = 100.1205
y = str(x)[6]
print("{:.{}f}".format(x,y))
x = 100.1205
y = str(x)[6] ## This is turning x into a string and grabbing the 6th character which is 0
print("{:.{}f}".format(x,y)) # this is saying print x to the decimal place
#specified by the value of y (zeroth digit) which is 100

how to extract digits from a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a list
a=['invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90']
i want to extract only digits from this list of the form
a=['2018-33167-2 03/21/2018 8,804.90']
i have used regex to extract
for i in a:
res = re.sub("\D", "", i)
but the result is
res=201833167203212018880490
Based on the expected output, you want more than just digits. The digits can have /, - and ,.
Maybe, perhaps, you wanted something that starts with and ends with a digit, but anything in the middle other than a space?
import re
a='invoice β invoice # 2018-33167-2 β date 03/21/2018 β total due $8,804.90'
print(re.findall(r'\d[^ ]+\d', a))
output
python3 test.py
['2018-33167-2', '03/21/2018', '8,804.90']

Python integer spacing [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a code like this.
print (' ', totalh, totalt, totalo)
and I want the integer of totalh to be 2 2 instead of 22 with a space in-between the integers. How do I go about doing it?
Convert it to a string which is an iterable and can be an argument to " ".join.
totalh = 22
print(" ".join(str(totalh)))
Output:
2 2
I suggest you this simple way: first convert totalh into a string with each digit being a character, then use " ".join() to insert a space between each digit:
totalh = 22
print(" ".join(str(totalh))) #2 2

Using python how to convert hex byte string to string of hex intergers in flipped order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
From:
25f207f28332c7da539dd2539230a7aad975f907a93d924b4b037abacde677f0f71f75cc250df2e4dd2be3e97a60f289
To:
0x7a60f289, 0xdd2be3e9, 0x250df2e4, 0xf71f75cc, 0xcde677f0, 0x4b037aba,
0xa93d924b, 0xd975f907, 0x9230a7aa, 0x539dd253, 0x8332c7da, 0x25f207f2
Just slice the string into chunks and reverse the order adding 0x with str.format:
s = "25f207f28332c7da539dd2539230a7aad975f907a93d924b4b037abacde677f0f71f75cc250df2e4dd2be3e97a60f289"
print(["0x{}".format(s[i-8:i]) for i in range(len(s), 0, -8) ])
['0x7a60f289', '0xdd2be3e9', '0x250df2e4', '0xf71f75cc', '0xcde677f0', '0x4b037aba', '0xa93d924b', '0xd975f907', '0x9230a7aa', '0x539dd253', '0x8332c7da', '0x25f207f2']
If you actually want decimals:
print([int("0x{}".format(s[i-8:i]),16) for i in range(len(s), 0, -8) ])
[2053173897, 3710641129, 621671140, 4146034124, 3454433264, 1258519226, 2839384651, 3648387335, 2452662186, 1402851923, 2201143258, 636618738]

Create random string with a char + 2 numbers - python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Two questions:
How to create a char+number random string. one char and 2 digit numbers
for example: U12 OR W82
How to create a random 5 digit number with no zero at the beginning: 02345 , 03243
1)
import random, string
"%c%02d" % (random.choice(string.uppercase), random.randint(0, 99))
2)
import random
random.randint(10000, 99999)

Categories

Resources