如何从 python 中的字符串中排除第一个字符(在随机生成的字符串中)
How do I exclude the first character if its 0 from a string in python (in a randomly generated string)
所以我是这个网站的新手,我正在尝试用我的代码解决一些问题。我正在尝试在 python 中制作一个 lightshot 截图抓取工具,但我发现任何以 0 开头的代码都会重定向到主页,我想避免这种情况。
我当前的代码和示例输出:
import string
import random
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k = N))
if not string.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
main()
>>>How many links would you like to input? Please put a number.3
>>>https://prnt.sc/(insert random string here, not here because there are scams on lightshot)
>>>https://prnt.sc/(random string)
>>>https://prnt.sc/(random string)
我试过查找并询问朋友,但我要么太累了,要么就是不知道该做什么。两者都有可能,因为我对 python 还很陌生。我可以在这里得到任何帮助吗?
你也在使用 string.startswith
你是说 r
因为那是你生成的字符串吗?
import string
import random
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k = N))
if not r.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
main()
或者,如果您的意思是您不想要第一个字符串(生成的要打印的数字),您可以更改您的 for 循环。
for i in range(1,x):
gen(x)
错误来自您的string.startswith("0")
字符串模块没有那个方法。
您需要对字符串对象调用 startswith() 方法。
根据您的代码,我假设您想对随机字符串使用 startswith r
这是运行的固定代码:
import random
import string
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k=N))
if not r.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
if __name__ == '__main__':
main()
所以我是这个网站的新手,我正在尝试用我的代码解决一些问题。我正在尝试在 python 中制作一个 lightshot 截图抓取工具,但我发现任何以 0 开头的代码都会重定向到主页,我想避免这种情况。 我当前的代码和示例输出:
import string
import random
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k = N))
if not string.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
main()
>>>How many links would you like to input? Please put a number.3
>>>https://prnt.sc/(insert random string here, not here because there are scams on lightshot)
>>>https://prnt.sc/(random string)
>>>https://prnt.sc/(random string)
我试过查找并询问朋友,但我要么太累了,要么就是不知道该做什么。两者都有可能,因为我对 python 还很陌生。我可以在这里得到任何帮助吗?
你也在使用 string.startswith
你是说 r
因为那是你生成的字符串吗?
import string
import random
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k = N))
if not r.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
main()
或者,如果您的意思是您不想要第一个字符串(生成的要打印的数字),您可以更改您的 for 循环。
for i in range(1,x):
gen(x)
错误来自您的string.startswith("0")
字符串模块没有那个方法。
您需要对字符串对象调用 startswith() 方法。
根据您的代码,我假设您想对随机字符串使用 startswith r
这是运行的固定代码:
import random
import string
def main():
def gen(x):
N = 6
r = ''.join(random.choices(string.ascii_lowercase + string.digits, k=N))
if not r.startswith("0"):
print("https://prnt.sc/" + str(r) + ' ' + f"{i}")
x = int(input("How many links would you like to generate? please input a number."))
for i in range(x):
gen(x)
q = input("Would you like to generate more? (say 'y' or 'n'.)")
if q == "y":
main()
else:
quit()
if __name__ == '__main__':
main()