Write a function that determine if the string is being supplied represents a correctly delimited Python...












0















For the purpose of this function a valid delimiter is the quotation mark(")or the apostrophe('). Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string. Your function should return the result as a Boolean value.



Here are examples of how your function should work.



test1 = input("Enter a test string: ") #user enters: "hello worlds"
print(valid_string(test1)) #True


It's from my previous test and I received a perfect zero. Can someone show me where to start with?










share|improve this question

























  • Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 22 '18 at 4:16
















0















For the purpose of this function a valid delimiter is the quotation mark(")or the apostrophe('). Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string. Your function should return the result as a Boolean value.



Here are examples of how your function should work.



test1 = input("Enter a test string: ") #user enters: "hello worlds"
print(valid_string(test1)) #True


It's from my previous test and I received a perfect zero. Can someone show me where to start with?










share|improve this question

























  • Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 22 '18 at 4:16














0












0








0








For the purpose of this function a valid delimiter is the quotation mark(")or the apostrophe('). Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string. Your function should return the result as a Boolean value.



Here are examples of how your function should work.



test1 = input("Enter a test string: ") #user enters: "hello worlds"
print(valid_string(test1)) #True


It's from my previous test and I received a perfect zero. Can someone show me where to start with?










share|improve this question
















For the purpose of this function a valid delimiter is the quotation mark(")or the apostrophe('). Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string. Your function should return the result as a Boolean value.



Here are examples of how your function should work.



test1 = input("Enter a test string: ") #user enters: "hello worlds"
print(valid_string(test1)) #True


It's from my previous test and I received a perfect zero. Can someone show me where to start with?







python-3.x function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:03









sai saran

358224




358224










asked Nov 22 '18 at 4:09









Bohan ChenBohan Chen

6




6













  • Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 22 '18 at 4:16



















  • Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 22 '18 at 4:16

















Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

– Andreas
Nov 22 '18 at 4:16





Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

– Andreas
Nov 22 '18 at 4:16












2 Answers
2






active

oldest

votes


















1














The key to figuring this out is here:




Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string.




You can check that the string begins with a double quote by doing something like my_string[0] == '"', and similarly for the end by doing my_string[-1] == '"'. This works because string[0] and string [-1] will access the first and last characters, respectively. In case you're not aware, negative indexes in Python just mean to start counting from the end (so index -2 would mean the second from the end, etc).



You can then check that no double-quote occurs inside of the string by doing something like this: my_string[1:-1].count('"') == 0. This works because my_string[1:-1] takes the part of the string excluding the first and last character, and then count hte number of times that the double quote occurs (zero- it should never occur).



But wait! The single quote can also be a delimiter! I'll leave that as a challenge for you. Hint: make sure the starting and ending delimiter are the same, and also make sure to allow the other delimiter to occur inside of the string. You should also think about handling edge cases with strings that are 0 or 1 characters long. Let me know if any parts of this answer don't make sense or if you would like clarification on something.






share|improve this answer





















  • 1





    I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

    – user2357112
    Nov 22 '18 at 4:33











  • I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

    – Unsolved Cypher
    Nov 22 '18 at 4:34



















0














Ok this should work:



def valid_string(s):
try:
return s[0] == s[-1] and s[0] in ["'", '"'] and s[0] not in s[1:-1] and len(s) > 1
except:
return False


while True:
test1 = input("Enter a test string: ")
print(valid_string(test1))


This works find for strings less that 2 characters long:



$ ./valid_string2.py
Enter a test string: 'adsdasfd"adfadfs'
True
Enter a test string: 'adsfdsafdfs'adsfasd'
False
Enter a test string: "adsfdasf'adsfads"
True
Enter a test string: "adsfasdf"adfasd"
False
Enter a test string: """
False
Enter a test string: "'"
True
Enter a test string: '"'
True
Enter a test string: a
False
Enter a test string: 'a'
True
Enter a test string: "a'
False
Enter a test string: a'
False
Enter a test string: 'a"
False





share|improve this answer


























  • You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

    – user2357112
    Nov 22 '18 at 4:54











  • I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

    – Red Cricket
    Nov 22 '18 at 5:01













  • Ah I think you mean embedded delimiter. I'll fix that.

    – Red Cricket
    Nov 22 '18 at 5:16











  • Now try entering an empty string (hit Enter without typing any characters).

    – user2357112
    Nov 22 '18 at 5:40











  • Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

    – Red Cricket
    Nov 22 '18 at 5:46













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423773%2fwrite-a-function-that-determine-if-the-string-is-being-supplied-represents-a-cor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The key to figuring this out is here:




Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string.




You can check that the string begins with a double quote by doing something like my_string[0] == '"', and similarly for the end by doing my_string[-1] == '"'. This works because string[0] and string [-1] will access the first and last characters, respectively. In case you're not aware, negative indexes in Python just mean to start counting from the end (so index -2 would mean the second from the end, etc).



You can then check that no double-quote occurs inside of the string by doing something like this: my_string[1:-1].count('"') == 0. This works because my_string[1:-1] takes the part of the string excluding the first and last character, and then count hte number of times that the double quote occurs (zero- it should never occur).



But wait! The single quote can also be a delimiter! I'll leave that as a challenge for you. Hint: make sure the starting and ending delimiter are the same, and also make sure to allow the other delimiter to occur inside of the string. You should also think about handling edge cases with strings that are 0 or 1 characters long. Let me know if any parts of this answer don't make sense or if you would like clarification on something.






share|improve this answer





















  • 1





    I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

    – user2357112
    Nov 22 '18 at 4:33











  • I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

    – Unsolved Cypher
    Nov 22 '18 at 4:34
















1














The key to figuring this out is here:




Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string.




You can check that the string begins with a double quote by doing something like my_string[0] == '"', and similarly for the end by doing my_string[-1] == '"'. This works because string[0] and string [-1] will access the first and last characters, respectively. In case you're not aware, negative indexes in Python just mean to start counting from the end (so index -2 would mean the second from the end, etc).



You can then check that no double-quote occurs inside of the string by doing something like this: my_string[1:-1].count('"') == 0. This works because my_string[1:-1] takes the part of the string excluding the first and last character, and then count hte number of times that the double quote occurs (zero- it should never occur).



But wait! The single quote can also be a delimiter! I'll leave that as a challenge for you. Hint: make sure the starting and ending delimiter are the same, and also make sure to allow the other delimiter to occur inside of the string. You should also think about handling edge cases with strings that are 0 or 1 characters long. Let me know if any parts of this answer don't make sense or if you would like clarification on something.






share|improve this answer





















  • 1





    I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

    – user2357112
    Nov 22 '18 at 4:33











  • I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

    – Unsolved Cypher
    Nov 22 '18 at 4:34














1












1








1







The key to figuring this out is here:




Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string.




You can check that the string begins with a double quote by doing something like my_string[0] == '"', and similarly for the end by doing my_string[-1] == '"'. This works because string[0] and string [-1] will access the first and last characters, respectively. In case you're not aware, negative indexes in Python just mean to start counting from the end (so index -2 would mean the second from the end, etc).



You can then check that no double-quote occurs inside of the string by doing something like this: my_string[1:-1].count('"') == 0. This works because my_string[1:-1] takes the part of the string excluding the first and last character, and then count hte number of times that the double quote occurs (zero- it should never occur).



But wait! The single quote can also be a delimiter! I'll leave that as a challenge for you. Hint: make sure the starting and ending delimiter are the same, and also make sure to allow the other delimiter to occur inside of the string. You should also think about handling edge cases with strings that are 0 or 1 characters long. Let me know if any parts of this answer don't make sense or if you would like clarification on something.






share|improve this answer















The key to figuring this out is here:




Correctly delimited strings must use the same delimiter for the beginning and end character, and that delimiter must not be used within the string.




You can check that the string begins with a double quote by doing something like my_string[0] == '"', and similarly for the end by doing my_string[-1] == '"'. This works because string[0] and string [-1] will access the first and last characters, respectively. In case you're not aware, negative indexes in Python just mean to start counting from the end (so index -2 would mean the second from the end, etc).



You can then check that no double-quote occurs inside of the string by doing something like this: my_string[1:-1].count('"') == 0. This works because my_string[1:-1] takes the part of the string excluding the first and last character, and then count hte number of times that the double quote occurs (zero- it should never occur).



But wait! The single quote can also be a delimiter! I'll leave that as a challenge for you. Hint: make sure the starting and ending delimiter are the same, and also make sure to allow the other delimiter to occur inside of the string. You should also think about handling edge cases with strings that are 0 or 1 characters long. Let me know if any parts of this answer don't make sense or if you would like clarification on something.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 16:58









E_net4 wishes happy holidays

12k63468




12k63468










answered Nov 22 '18 at 4:22









Unsolved CypherUnsolved Cypher

495314




495314








  • 1





    I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

    – user2357112
    Nov 22 '18 at 4:33











  • I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

    – Unsolved Cypher
    Nov 22 '18 at 4:34














  • 1





    I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

    – user2357112
    Nov 22 '18 at 4:33











  • I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

    – Unsolved Cypher
    Nov 22 '18 at 4:34








1




1





I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

– user2357112
Nov 22 '18 at 4:33





I'm not the downvoter, but you seem to have forgotten to handle length-1 and length-0 inputs.

– user2357112
Nov 22 '18 at 4:33













I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

– Unsolved Cypher
Nov 22 '18 at 4:34





I did omit these cases, but the question asks for a starting point for writing such a function, not for a complete solution (otherwise I would have posted code). Thanks for pointing that out, though- I will add a note about that.

– Unsolved Cypher
Nov 22 '18 at 4:34













0














Ok this should work:



def valid_string(s):
try:
return s[0] == s[-1] and s[0] in ["'", '"'] and s[0] not in s[1:-1] and len(s) > 1
except:
return False


while True:
test1 = input("Enter a test string: ")
print(valid_string(test1))


This works find for strings less that 2 characters long:



$ ./valid_string2.py
Enter a test string: 'adsdasfd"adfadfs'
True
Enter a test string: 'adsfdsafdfs'adsfasd'
False
Enter a test string: "adsfdasf'adsfads"
True
Enter a test string: "adsfasdf"adfasd"
False
Enter a test string: """
False
Enter a test string: "'"
True
Enter a test string: '"'
True
Enter a test string: a
False
Enter a test string: 'a'
True
Enter a test string: "a'
False
Enter a test string: a'
False
Enter a test string: 'a"
False





share|improve this answer


























  • You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

    – user2357112
    Nov 22 '18 at 4:54











  • I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

    – Red Cricket
    Nov 22 '18 at 5:01













  • Ah I think you mean embedded delimiter. I'll fix that.

    – Red Cricket
    Nov 22 '18 at 5:16











  • Now try entering an empty string (hit Enter without typing any characters).

    – user2357112
    Nov 22 '18 at 5:40











  • Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

    – Red Cricket
    Nov 22 '18 at 5:46


















0














Ok this should work:



def valid_string(s):
try:
return s[0] == s[-1] and s[0] in ["'", '"'] and s[0] not in s[1:-1] and len(s) > 1
except:
return False


while True:
test1 = input("Enter a test string: ")
print(valid_string(test1))


This works find for strings less that 2 characters long:



$ ./valid_string2.py
Enter a test string: 'adsdasfd"adfadfs'
True
Enter a test string: 'adsfdsafdfs'adsfasd'
False
Enter a test string: "adsfdasf'adsfads"
True
Enter a test string: "adsfasdf"adfasd"
False
Enter a test string: """
False
Enter a test string: "'"
True
Enter a test string: '"'
True
Enter a test string: a
False
Enter a test string: 'a'
True
Enter a test string: "a'
False
Enter a test string: a'
False
Enter a test string: 'a"
False





share|improve this answer


























  • You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

    – user2357112
    Nov 22 '18 at 4:54











  • I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

    – Red Cricket
    Nov 22 '18 at 5:01













  • Ah I think you mean embedded delimiter. I'll fix that.

    – Red Cricket
    Nov 22 '18 at 5:16











  • Now try entering an empty string (hit Enter without typing any characters).

    – user2357112
    Nov 22 '18 at 5:40











  • Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

    – Red Cricket
    Nov 22 '18 at 5:46
















0












0








0







Ok this should work:



def valid_string(s):
try:
return s[0] == s[-1] and s[0] in ["'", '"'] and s[0] not in s[1:-1] and len(s) > 1
except:
return False


while True:
test1 = input("Enter a test string: ")
print(valid_string(test1))


This works find for strings less that 2 characters long:



$ ./valid_string2.py
Enter a test string: 'adsdasfd"adfadfs'
True
Enter a test string: 'adsfdsafdfs'adsfasd'
False
Enter a test string: "adsfdasf'adsfads"
True
Enter a test string: "adsfasdf"adfasd"
False
Enter a test string: """
False
Enter a test string: "'"
True
Enter a test string: '"'
True
Enter a test string: a
False
Enter a test string: 'a'
True
Enter a test string: "a'
False
Enter a test string: a'
False
Enter a test string: 'a"
False





share|improve this answer















Ok this should work:



def valid_string(s):
try:
return s[0] == s[-1] and s[0] in ["'", '"'] and s[0] not in s[1:-1] and len(s) > 1
except:
return False


while True:
test1 = input("Enter a test string: ")
print(valid_string(test1))


This works find for strings less that 2 characters long:



$ ./valid_string2.py
Enter a test string: 'adsdasfd"adfadfs'
True
Enter a test string: 'adsfdsafdfs'adsfasd'
False
Enter a test string: "adsfdasf'adsfads"
True
Enter a test string: "adsfasdf"adfasd"
False
Enter a test string: """
False
Enter a test string: "'"
True
Enter a test string: '"'
True
Enter a test string: a
False
Enter a test string: 'a'
True
Enter a test string: "a'
False
Enter a test string: a'
False
Enter a test string: 'a"
False






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 5:48

























answered Nov 22 '18 at 4:52









Red CricketRed Cricket

4,36993384




4,36993384













  • You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

    – user2357112
    Nov 22 '18 at 4:54











  • I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

    – Red Cricket
    Nov 22 '18 at 5:01













  • Ah I think you mean embedded delimiter. I'll fix that.

    – Red Cricket
    Nov 22 '18 at 5:16











  • Now try entering an empty string (hit Enter without typing any characters).

    – user2357112
    Nov 22 '18 at 5:40











  • Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

    – Red Cricket
    Nov 22 '18 at 5:46





















  • You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

    – user2357112
    Nov 22 '18 at 4:54











  • I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

    – Red Cricket
    Nov 22 '18 at 5:01













  • Ah I think you mean embedded delimiter. I'll fix that.

    – Red Cricket
    Nov 22 '18 at 5:16











  • Now try entering an empty string (hit Enter without typing any characters).

    – user2357112
    Nov 22 '18 at 5:40











  • Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

    – Red Cricket
    Nov 22 '18 at 5:46



















You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

– user2357112
Nov 22 '18 at 4:54





You didn't handle checking non-endpoint characters, or strings shorter than 2 characters.

– user2357112
Nov 22 '18 at 4:54













I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

– Red Cricket
Nov 22 '18 at 5:01







I am not sure what a non-endpoint character is. And it works fine for strings with length less that two.

– Red Cricket
Nov 22 '18 at 5:01















Ah I think you mean embedded delimiter. I'll fix that.

– Red Cricket
Nov 22 '18 at 5:16





Ah I think you mean embedded delimiter. I'll fix that.

– Red Cricket
Nov 22 '18 at 5:16













Now try entering an empty string (hit Enter without typing any characters).

– user2357112
Nov 22 '18 at 5:40





Now try entering an empty string (hit Enter without typing any characters).

– user2357112
Nov 22 '18 at 5:40













Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

– Red Cricket
Nov 22 '18 at 5:46







Is that in the output already? Let me check. Yep that needs to be handled. Updated answer.

– Red Cricket
Nov 22 '18 at 5:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423773%2fwrite-a-function-that-determine-if-the-string-is-being-supplied-represents-a-cor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen